Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 03-07-2008
shamrock shamrock is offline Forum Advisor  
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 759
Quote:
Originally Posted by ramkrix View Post
Thanks for your reply shamrock..

Is tis the way, I need to include the condition in while loop:

while(feof(stream)==0)

One more question to you: instead of using command line args and C library fns can we check this by having the program absolute system calls..

Thanks in advance,
Ramkrix
You can use "absolute system calls" instead of standard C library routines like fopen() and you can avoid passing the input filename as a command line argument at the expense of hardcoding the input filename in the open() system call.

The system call approach is better for reading 5 bytes at a time from the input file and printing the fifth byte to standard output. This method is preferred over incrementing a counter and repeatedly testing if x < 5 or checking for EOF using the feof() standard lib function.


Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

main(int argc, char *argv[])
{
    int fd;
    char name[5];

    fd = open("/path/to/input/file", O_RDONLY);

    while (read(fd, (void *) name, (size_t) 5) == 5)
        printf("the fifth byte is %c\n", name[4]);
}