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.
|
#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);
}
|