Using the skeleton below
#include <unistd.h> // read/write
#include <sys/file.h> // open/close values
#include <string.h> // strlen
int main( int argc, char *argv[], char *env[] )
{
// C++ or C code
}
Write a C++ application myrm that removes (deletes) files passed as command line
argument. Use only the Unix/Linux API in your program, do not use standard library
functions.
echo > File1
./myrm File1
I've gone through several tutorials on the linux API and I'm fairly competent with C++. My guess is that i need to be able to call a method (sorry, i'm used to java) that deletes files? Any help truly appreciated
Dave
Help with c++
Started by Dave256000, Feb 28 2007 10:01 AM
7 replies to this topic
#1
Posted 28 February 2007 - 10:01 AM
|
|
|
#2
Posted 28 February 2007 - 11:46 PM
So you can't use:
std::remove(arg[0]);
#3
Posted 01 March 2007 - 02:45 AM
Thankyou for your reply, but don't forget somewhere in there I have to write an application myrm that deletes the given file. So that already gives away that I have to use the rm command.
I just can't get my head around how I fit that into the skeleton provided. Can I pass files to the rm command as a parameter? and if so, why is the skeleton structured like that?
Also, how do I call an application like that from that c++ skeleton?
Someone, throw me a bone here!
Dave
I just can't get my head around how I fit that into the skeleton provided. Can I pass files to the rm command as a parameter? and if so, why is the skeleton structured like that?
Also, how do I call an application like that from that c++ skeleton?
Someone, throw me a bone here!
Dave
#4
Posted 02 March 2007 - 09:38 AM
It sounds like you need to use the system function.
#5
Posted 10 March 2007 - 06:29 AM
>So that already gives away that I have to use the rm command.
Why? Just because your program has 'rm' in the name doesn't mean you have to implement it using rm. In fact, using system to run rm is kind of dumb logically because you're writing a program to run in shell and the program goes back to the shell to run another program that does exactly the same thing. Why not just use rm from the shell and not waste your time with myrm? ;)
>Someone, throw me a bone here!
You have two smart choices. First, you could use the standard C library remove function:
The other choice is to go standard POSIX and use unlink:
>why is the skeleton structured like that?
Because it's more complicated than it needs to be and includes unnecessary elements.
Why? Just because your program has 'rm' in the name doesn't mean you have to implement it using rm. In fact, using system to run rm is kind of dumb logically because you're writing a program to run in shell and the program goes back to the shell to run another program that does exactly the same thing. Why not just use rm from the shell and not waste your time with myrm? ;)
>Someone, throw me a bone here!
You have two smart choices. First, you could use the standard C library remove function:
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
while ( [I]<loop over argv>[/I] )
remove ( [I]<current argv>[/I] );
return 0;
}
This is the best choice if you want portable code because this program can now be used outside of a POSIX system and still work without changing the code in any way.The other choice is to go standard POSIX and use unlink:
#include <unistd.h>
int main ( int argc, char *argv[] )
{
while ( [I]<loop over argv>[/I] )
unlink ( [I]<current argv>[/I] );
return 0;
}
The logic is identical because remove was designed to mimic unlink. In fact, your libc's remove probably uses unlink internally.>why is the skeleton structured like that?
Because it's more complicated than it needs to be and includes unnecessary elements.
#6
Posted 11 March 2007 - 10:32 AM
yeh thanks goddess, you were right. duno why the code was structured like that, it was an exercise in familiarising ourselves with linux api calls.
Reckon I can ask for your help if i get stuck again?
Dave
Reckon I can ask for your help if i get stuck again?
Dave
#7
Posted 11 March 2007 - 11:02 AM
>Reckon I can ask for your help if i get stuck again?
Of course, that's why I'm here. :)
Of course, that's why I'm here. :)
#8
Guest_teenachacko_*
Posted 23 March 2007 - 02:21 AM
Guest_teenachacko_*
Dave256000 said:
Using the skeleton below
#include <unistd.h> // read/write
#include <sys/file.h> // open/close values
#include <string.h> // strlen
int main( int argc, char *argv[], char *env[] )
{
// C++ or C code
}
Write a C++ application myrm that removes (deletes) files passed as command line
argument. Use only the Unix/Linux API in your program, do not use standard library
functions.
echo > File1
./myrm File1
I've gone through several tutorials on the linux API and I'm fairly competent with C++. My guess is that i need to be able to call a method (sorry, i'm used to java) that deletes files? Any help truly appreciated
Dave
#include <unistd.h> // read/write
#include <sys/file.h> // open/close values
#include <string.h> // strlen
int main( int argc, char *argv[], char *env[] )
{
// C++ or C code
}
Write a C++ application myrm that removes (deletes) files passed as command line
argument. Use only the Unix/Linux API in your program, do not use standard library
functions.
echo > File1
./myrm File1
I've gone through several tutorials on the linux API and I'm fairly competent with C++. My guess is that i need to be able to call a method (sorry, i'm used to java) that deletes files? Any help truly appreciated
Dave
I want source code for dictionary implementation using files in c++


Sign In
Create Account

Back to top









