Question

In: Computer Science

C11 standard.... file1.txt: DAY Blackberry Blueberry Pear Mango apple Monday 60 35 32 16 30 Tuesday...

C11 standard....

file1.txt:

DAY Blackberry Blueberry Pear Mango apple
Monday 60 35 32 16 30
Tuesday 44 22 34 62 54
Wednesday 33 14 67 13 22
Thursday 24 44 35 15 42
Friday 36 43 24 34 29

file2.txt:

DAY Milk Cheese Butter Cream Yogurt
Monday 50 25 32 16 512
Tuesday 64 12 34 62 97
Wednesday 23 54 67 13 62
Thursday 34 54 25 35 127
Friday 76 23 54 24 120

Here, in the text file, the first line is column title contains less than 100 characters.

Each subsequent line contains six data items separated by space:

-The day of the week as a string containing no spaces with a length of less than 25 characters.
- Five additional items in each line that are the numbers of each food item is sold in as a string with length less than 15 characters.

-The number of columns in the file is fixed at 6.

TO DO-

Read the two text files and swap the data in the Apple column of file1.txt with Yogurt column of file2.txt and write the corrected data in file1-revised.txt and file2-revised.txt.

Assume that Apple and Yogurt is in the last column of file1.txt and file2.txt. Column title should not be modified.

Use feof() function to detect the end of lines.

Number of columns are fixed but number of rows can vary.

Solutions

Expert Solution

Code for your program is provided below. It is explained thoroughly in code comments. You can also refer to the screenshot of properly indented code in IDE that i have attached in the last. Output screenshot is also provided.

If you need any further clarification , please feel free to ask in comments. I would really appreciate if you would let me know if you are satisfied with the explanation provided.

#################################################################

CODE

#include <stdio.h>   //for standard input output
#include <stdlib.h>  //for using exit()

int main()
{

   FILE *myFile1;  //file pointer for first file
   FILE *myFile2;   //file pointer for second file
   FILE *outputFile1;   //file pointer for first modified file
   FILE *outputFile2;   //file pointer for second modified file
   myFile1=fopen("file1.txt","r");   //opening first file in read mode
   myFile2=fopen("file2.txt","r");   //opening second file in read mode

   if(myFile1==NULL)  //if file can not b opened
   {
       printf("File can not be Opened. Terminating the Program.");
       exit(0);  //exit
   }

   if(myFile2==NULL)   //if second file can not be opened out of  any reason
   {
       printf("File can not be Opened. Terminating the Program.");
       exit(0);  //exit the program
   }

   outputFile1=fopen("file1-revised.txt","w");  //create first modified file in write mode
   outputFile2=fopen("file2-revised.txt","w");  //create second modified file in write mode

   if(outputFile1==NULL)   //if first modified file can not be created
   {
       printf("File can not be Created. Terminating the Program.");
       exit(0);
   }

   if(outputFile2==NULL)   //if second modified file can not be created
   {
       printf("File can not be Created. Terminating the Program.");
       exit(0);   //exit the program
   }

   char headings1[100];  //to store headings of first file
   char headings2[100];  //to store headings of second file
   char file1Day[25];  //to store day of first file
   char file2Day[25];  //to store day of second file
   int file1Val2,file1Val3,file1Val4,file1Val5,file1Val6;  //to store rest of the value of file 1
   int file2Val2,file2Val3,file2Val4,file2Val5,file2Val6;   //to store rest of the value of file 2

   fgets(headings1,100,myFile1);  //read first line of first file, these are the headings
   fputs(headings1,outputFile1);  //read first line of second file, these are the headings

   fgets(headings2,100,myFile2);  //write headings of first file into modified first file
   fputs(headings2,outputFile2);  //write headings of second file into modified second file

   while(1)  //loop to read files and write into modified files
   {
       //read first file and store individual values into different variables
       fscanf(myFile1,"%s %d %d %d %d %d %d",file1Day,&file1Val2,&file1Val3,&file1Val4,&file1Val5,&file1Val6);
       //read second  file and store individual values into different variables
       fscanf(myFile2,"%s %d %d %d %d %d %d",file2Day,&file2Val2,&file2Val3,&file2Val4,&file2Val5,&file2Val6);
        //write into first modified file all the first five columns of first file , and replace the last column with second file value ie, file2Val6
       fprintf(outputFile1,"%s %d %d %d %d %d\n",file1Day,file1Val2,file1Val3,file1Val4,file1Val5,file2Val6);
      //write into second modified file all the first five columns of second file, and replace the last column by first file value ie, file1Val6
       fprintf(outputFile2,"%s %d %d %d %d %d\n",file2Day,file2Val2,file2Val3,file2Val4,file2Val5,file1Val6);

       if(feof(myFile1))  //if end of file1 is reached
        break;  //break out of loop

       if(feof(myFile2))   //if end of file2 is reached
        break;  //break out of loop
    }

    return 0;
}

##########################################################

OUTPUT

file1-revised.txt

file2-revised.txt

###################################################################

SCREENSHOT OF CODE IN IDE


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT