C++ and the Linux API

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 3
Reputation: Dave256000 is an unknown quantity at this point 
Solved Threads: 0
Dave256000 Dave256000 is offline Offline
Newbie Poster

C++ and the Linux API

 
0
  #1
Mar 4th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ and the Linux API

 
0
  #2
Mar 4th, 2007
>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 unlink ( argv[i] );, which would be a much better solution:
  1. #include <unistd.h> // read/write
  2.  
  3. int main( int argc, char *argv[] )
  4. {
  5. int i;
  6.  
  7. for ( i = 1; i < argc; i++ )
  8. unlink ( argv[i] );
  9.  
  10. return 0;
  11. }
>Does this involve using the tail command?!
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: Dave256000 is an unknown quantity at this point 
Solved Threads: 0
Dave256000 Dave256000 is offline Offline
Newbie Poster

Re: C++ and the Linux API

 
0
  #3
Mar 4th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC