1.Write a C program that take file names as command line arguments. It will read
ID: 3533157 • Letter: 1
Question
1.Write a C program that take file names as command line arguments. It will read each named file and write the first 10 lines to standard output. If there are no command line arguments, it will read from standard input. If "-" is given as a file name, it will read from standard input.
As with the Linux version of head, your program should print the file name (or standard input) if there are two or more command line arguments. Use the same format as the Linux version of head.
You must write a function void do_file(FILE *f) that takes a stream and processes that stream. READ THESE WORDS CAREFULLY: IF YOU PUT THE ENTIRE FUNCTIONALITY OF THE PROGRAM INTO THIS FUNCTION, I WILL NOT GRADE YOUR PROGRAM!!! You must open the files in main and call do_file to process them.
You must check to make sure that files open properly and print appropriate error messages to stderr (in the usual Unix style - using errno) if not. If a file cannot be opened, go ahead and process the other files. Also
It must handle the options -q (never print file headers), -v (always print file headers), -n k (print the first k lines of the file rather than the first 10), and -c k (print the first k bytes of the file rather than the first 10 lines). Although the man page for head allows negative values for k (e.g. meaning print all but the last k lines), you do not have to implement this feature. The man page also allows for a "multiplier suffix": e.g. -n 10M, which means 10 megabytes (10*2^20). You do have to implement this feature, but only for the following suffixes: b, K, and M. Read the man page to find out the meanings.
Note that to get the value for k, you must use optarg, which is the argument (a string) associated with an option. For example, limit = atoi(optarg), which needs to go with the -n case and the -c case as you process options.
2. Write a simplified version of sort called my-sort-1 that reads from standard input, sorts lexicographically, and writes the output to standard output. For this program you may assume that the longest line has 1022 characters and that there are at most 4096 lines. Use an array of characters as a buffer to read a line. However, after you read a line, you must dynamically allocate a string of the right size to hold it (strdup). Use an array of strings to hold all of the lines. To sort the data, use qsort. Do NOT try to write a sort function!
Explanation / Answer
#include int main ( int argc, char *argv[] ) { printf("Enter the file name: "); //scanf if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv[0] assuming it is the program name */ printf( "usage: %s filename", argv[0] ); } else { // We assume argv[1] is a filename to open FILE *file = fopen( argv[1], "r" ); /* fopen returns 0, the NULL pointer, on failure */ if ( file == 0 ) { printf( "Could not open file " ); } else { int x; /* read one character at a time from file, stopping at EOF, which indicates the end of the file. Note that the idiom of "assign to a variable, check the value" used below works because the assignment statement evaluates to the value assigned. */ while ( ( x = fgetc( file ) ) != EOF ) { printf( "%c", x ); } fclose( file ); } } return 0;}Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.