C++ allows statements of the form #include filename which reads filename and ins
ID: 3561084 • Letter: C
Question
C++ allows statements of the form
#include filename
which reads filename and inserts its contents in place of the include statement. Include statements may be nested; in other words, the file filename may itself contain an include statement, but, a file cant include itself in any chain.Write a program that reads in a file and outputs the file as modified by the include statements.
Solution
The general way to do this is to write a procedure with heading
void processFile( String fileName );
which opens fileName, does whatever processing is needed, and then closes it. If a line of the form
#include SomeFile
is detected, then the call
processFile( SomeFile );
is made recursively. Self-referential includes can be detected by keeping a list of files for which a call to processFile has not yet terminated, and checking this list before making a new call to processFile.
Question: I dont understand the connection between the question and the answer. Please demonstrate answer by writeing the program so that I understand what it is talking about. I am lost, really lost
Explanation / Answer
#include #include int main() { char c[1000]; FILE *fptr; fptr=fopen("program.txt","w"); if(fptr==NULL){ printf("Error!"); exit(1); } printf("Enter a sentence: "); gets(c); fprintf(fptr,"%s",c); fclose(fptr); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.