Question: in C Open file.c. Extend it so it reads all data from data.txt and writes this data to a new file. The name of the

in C

Open file.c. Extend it so it reads all data from data.txt and writes this data to a new file. The name of the new file should be requested from the user.

Note that because the default working directory is cmake-build-debug, the new generated file will be there as well unless a path elsewhere is specified. For example, if you want the new file to be in the src directory, you could take the user's entry of "data2.txt" and create a file ../src/data2.txt.

Data should be copied from data.txt into the new file (with the users provided name) one character at a time. All files should be closed before the program ends!

You may want to explore the manual pages for:

  • fgetc
  • fputc
  • fopen
  • fclose
  • fscanf

File.c

#include  int main(void) { /* file handles */ FILE *outputFile=NULL; /* open files for writing*/ outputFile = fopen("cwork.dat", "w"); if(outputFile == NULL) return(1); /* need to do explicit ERROR CHECKING */ /* write some data into the file */ fprintf(outputFile, "Hello there"); /* dont forget to close file handles */ fclose(outputFile); return 0; }

data.txt

There are no mistakes here, only happy accidents.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!