Printf First Line

Code Id 8
Date Updated 3/7/2010
Title printf first line  
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

void die(void)

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();
  } while (c != 'n');


Comments are closed.