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
#includeint 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started