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

Description: You are required to write a C program • whose input is an existing

ID: 3595299 • Letter: D

Question

Description:

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.

Suggestions: Use fflush(stdout) after each call to printf.

Explanation / Answer

/*
============================================================================
Name        : FileOp.c
Author      :
Version     :
Copyright   : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[]) {

   //open input file
   char str[20]={0};
   strcpy(str,argv[1]);
   strcat(str,".txt");
   FILE *ip=fopen(str,"r");

   if(ip==NULL)
   {
       printf("Input file does not exist ");
   exit(-1);
   }
   //open or create output file
   FILE *op;
   if(argv[2]!=NULL) //checking if output file exist
   {
   strcpy(str,argv[2]);
   strcat(str,".txt");
   op=fopen(str,"w");
   }
   else
   {
       op=fopen("output.txt","w");
   }

   char buffer[255]={0};
   while(fgets(buffer,sizeof(buffer),ip)) //read till end of input file
   {
       char wstr[255]={0};
       char *ptr=strtok(buffer," "); //tokenise read string
       while(ptr)
       {
           strcat(wstr,ptr);
           strcat(wstr," "); //pad space
           ptr=strtok(NULL," ");
       }
       fprintf(op,"%s",wstr); //print to output file

   }

   printf("Finished ");
   fclose(ip); //close ip file
   fclose(op); //close output file
   return EXIT_SUCCESS;
}