Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ and the linux API (http://www.go4expert.com/forums/cpp-linux-api-t3241/)

Dave256000 4Mar2007 15:41

C++ and the linux API
 
Using the skeleton below
Code:

#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

The answer for this turned out to be:
Code:

#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
    int i;
    for( i = 1; i < argc; i++ )
    {
        syscall( 10, argv[i] );
    }
    return 0;
}

Could anyone explain this to me, I'm having trouble understanding it. And if anyone's feeling really generous:

using the same skeleton

Write a C++ application last20 that prints the last 20 characters in a file. Use only
the Unix/Linux API in your program, do not use standard library functions.

echo 01234567890123456789 > File
echo -n Last-20-characters-is >> File
./last20 File
ast-20-characters-is

Does this involve using the tail command?!

Much appreciated,

Dave

shabbir 5Mar2007 09:40

Re: C++ and the linux API
 
I am not a Linux expert but as I can understand syscall( 10, argv[i] ); deletes the files passed as command line.


All times are GMT +5.5. The time now is 19:50.