The program starts by printing your name with an end sign For example, \"Jim X.
ID: 441583 • Letter: T
Question
The program starts by printing your name with an end sign For example, "Jim X. Chen >"; No matter what an input line is, the program print: "Do you want to use my vi to edit a file: test.txt? Y or N"; If "Y" of "y", print out "OK, I've made a copy of your file at: test1 txt"; your program will open "test.txt" for reading, and open "test1 txt" for writing; write a head line into "test1 txt" saying that "The following is a copy from test.txt"; copy the contents from "text.txt" to "test1 txt", and your program exit. If "N" or "n", print out "OK. If otherwise, printout "Error ", and your program go to Step 2. Here are some other requirements: You should use functions, for example, one for checking the input and another for handling the two files; You should have comments which explain what the functions do; Each function cannot be over 100 lines of code except the main function.Explanation / Answer
#include void filehandling()//copy contents from one file to another { FILE *fp1,*fp2; char c; printf("OK, I've made a copy of your file at: test1.txt "); fp1=fopen("test.txt","r"); fp2=fopen("test1.txt","w"); fprintf(fp2,"The following is a copy from test.txt "); while(1) { c=fgetc(fp1); if(feof(fp1)) break; fputc(c,fp2); } fcloseall(); } char getinput()//Read and return input { char input[100],choice; printf("NAME> "); scanf("%s",input); while(getchar()!=' '); printf("Do you want to use my vi to edit a file: test.txt? Y or N "); scanf("%c",&choice); return(choice); } main() { while(1) { char choice=getinput(); switch(choice) { case 'y': case 'Y':{ filehandling(); return; } case 'N': case 'n': { printf("OK. I will work on a vi system "); return; } default: printf("Error "); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.