| |||||||||||
Reading files in C using system calls Posted by hjortur (hjortur), 11 November 2007 Hi.I have been reading through Advanced Unix programming and surfing through the net seeking a way to read a simple text file from my harddrive and print it on my screen using system call and come up with no clear examples Is there any one who can point me in the rigth direction? Posted by george_Ball (george), 11 November 2007 Try this...#include <unistd.h> #include <fcntl.h> #define BUFSIZE 1024 int main () { char buffer [BUFSIZE]; int length; int fd; /* Open the file, print error message if we fail */ if ( ( fd = open ("data", O_RDONLY) ) < 0 ) { perror("Unable to open data"); exit (1); } /* Copy file contents to stdout, stop when read returns 0 (EOF) */ while ( (length = read (fd, buffer, BUFSIZE)) > 0 ) write (1, buffer, length); close (fd); exit (0); } Name of the file is the command line argument. It reads the file 1024 bytes at a time and prints on standard output (normally console). It's basically the functionality of cat, minus a few bells and whistles. Posted by hjortur (hjortur), 12 November 2007 Tks...This helped However I am trying to print a file in this format: syscall: this is the syscall: text i am syscall: trying to syscall: write instead of: this is the test i am trying to write How do I seperate the lines from the file...that is read one line at a time? Posted by hjortur (hjortur), 12 November 2007 I am also trying to ping a list of host that I read in from a file and print out if they are reachable or not.. contact=system("ping ?"); However I have not found out how to execute contact=system("ping") with a variable containing each host I want to ping. Can you put a variable into the system("") command or am I trying something that can't be done? Posted by george_Ball (george), 12 November 2007 However I am trying to print a file in this format: syscall: this is the ... instead of: this is the ... How do I seperate the lines from the file...that is read one line at a time? --------- Well the Unix system calls cannot read in a "line" at a time, they do not recognise that level of structure in a file. However you can use the higher level stdio library functions, in particular the fgets() function... #define BUFSIZE 128 ... char buffer[BUFSIZE]; char *bufp = buffer; FILE *fp; ... fp = fopen( filename, "r" ); if ( fp == (FILE *)0 ) /*error*/ while ( (bufp = fgets(bufp, BUFSIZE, fp) ) != (char *)0 ) { printf("syscall: %s\n", bufp); } I think that should work!! Posted by george_Ball (george), 12 November 2007 Can you put a variable into the system("") command or am I trying something that can't be done? ------- Indeed it can be done... Suppose you have the name of the host you need to ping in the variable hostname, then: char *hostname = ... /* fill in the hostname somehow */ char cmdbuf[256]; char *cmd = cmdbuf; sprintf(cmd, "ping %s", hostname); return = system(cmd); if ( return == 0 ) /* ping command succeeded */ else /* ping failed, maybe host unreachable or maybe something else */ Notice that the return value from the call to system() is the exit status of the ping command, which by convention is 0 if the command succeeded!! This rather strange feature is a part of standard Unix behaviour. Notice also that the command will share standard out with your program, which means that you will see all the messages from ping mixed in with your own output. You probably don't want that, so it may be worthwhile changing the command string to: sprintf ( cmd, "ping %s > /dev/null", hostname ) which arranges to throw away all output from the ping command... Posted by hjortur (hjortur), 13 November 2007 Tks alot....! Will try this out now... H This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |