Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are required to write a C program - whose input is an existing text file wit

ID: 3592790 • Letter: Y

Question

You are required to write a C program

- whose input is an existing text file with a name specified in a command line as the first argument,

- whose output is a new text file with a name specified in a command line as the second argument; requirements to the new text file are given below.

Input and output files should have extension ‘.txt’ but only names of the files are specified in a command line.

Example: suppose the executable version of your program is named p2.out. The program will be executed by a command line of the following form:

p2.out inputfile outputfile

In this case file inputfile.txt must exist, outputfile.txt will be created.

Requirements for the output text file:

The output file should have copy of each line of the input file in the same order of lines. Each line of the output file should consist of the same words as the corresponding line in the input file but ordered backward.

In the input file words are separated by one or more spaces (' ') or by horizontal tab (' '). In the output file words should be separated by single space.

In the input and output files, each line ends with new line character (‘ ’). Lines do not exceed 255 characters including new line character.

The outline of your C program is as follows.

1.Read command line arguments and check the correctness.

2.Create a new file named second argument + ".txt"

3.Proceed with converting input file lines into output file lines.

4.Stop and print out "Finished." once finish reading and writing the files.

Thus, each time your program is executed, it should handle just one input file.

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; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote