| | |
C++ and the Linux API
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Solved Threads: 0
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
The answer for this turned out to be:
#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?!
any help much 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
The answer for this turned out to be:
#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?!
any help much appreciated,
Dave
>syscall( 10, argv[i] );
This says to send the interrupt 10 (which references the unlink operation) to the system for processing. It's directly equivalent to saying
>Does this involve using the tail command?!
tail doesn't have a matching C interface. Try saying
This says to send the interrupt 10 (which references the unlink operation) to the system for processing. It's directly equivalent to saying
unlink ( argv[i] );, which would be a much better solution: C++ Syntax (Toggle Plain Text)
#include <unistd.h> // read/write int main( int argc, char *argv[] ) { int i; for ( i = 1; i < argc; i++ ) unlink ( argv[i] ); return 0; }
tail doesn't have a matching C interface. Try saying
man popen if you want to use it. Last edited by Narue; Mar 4th, 2007 at 8:49 am.
I'm here to prove you wrong.
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Solved Threads: 0
thanks, that's actually really helpful. so where can i find a list/examples of this type of code? as i can only use the linux api system calls for these questions.
also, any tips on the 'last 20' question? how would i find the system equivalent for tail()? because it would be tail that i'd use right?
thank you for your reply, you've enlightened me!
Dave
also, any tips on the 'last 20' question? how would i find the system equivalent for tail()? because it would be tail that i'd use right?
thank you for your reply, you've enlightened me!
Dave
![]() |
Similar Threads
- help me?! (C++)
- Is assembly worth learning it? (Assembly)
- Linux API - how to use it (C)
- which lang to choose (C++)
- Programming in Linux? (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Calculator project
- Next Thread: Class Error
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






