Printf First Line

Code Id 8
Date Updated 3/7/2010
Title printf first line  
Description
Write a firstline program that, given a command-line argument, prints the first line of the file with that name.
For example, firstline data.txt will print the first line of the file named data.txt. If a command-line argument is not supplied,
or if there is any problem reading the file, or if the file does not contain a line,print a message on stderr and exit. 
                                  
Codes Snippet
#include 

void die(void)
{
  fprintf(stderr,"Oops.n");
  exit(111);
}

int main(int argc,char **argv)
{
  FILE *f;
  char c;

  if (!argv[1]) die();

  f = fopen(argv[1],"r");
  if (!f) die();

  do {
    if (fscanf(f,"%c",&c) == -1) die();
    putchar(c);
  } while (c != 'n');

  exit(0);
}

Comments are closed.