ENCORE5
PAUSE
SOMECLEAN
ENCORE1
ENCORE
BOX
SOLUTION
GO
Let's look at an example together!
Step 1 is to establish a clear goal.
The primary goal is to: Copy the source file into the
destination file.
The secondary goal is to:
Learn how to use various functions
for text processing.
The file copying process may be represented as:
This is a
data file
copied
in this
example
Source File
--------> This is a
--------> data file
--------> copied
--------> in this
--------> example
Destination File
Since the source and destination file names will be entered
by the user, it is necessary to declare the buffers for them,
and include the standard I/O file, i.e:
#include <stdio.h>
main() /* copy file1 into file2 */
{
char file_in[25]; /* file names */
char file_out[25];
:
:
:
:
}
The character buffer is c and the function for
reading single characters is *gets().
int c;
char *gets(); /* reads a string from the file */
In the process of file copying, the files need
to be opened and file pointers will be required
for every file. The function fopen() and the
file pointers should be of type _______.
Answer here:
That is correct!
You've got it on the 2nd try.
Good! Now you understand the concept!
It is a user defined data type in stdio.h
They return a pointer to a file.
Please review Topic 2: Text File Concepts
FORGET4
The correct answer is FILE *! By definition, the file pointer
is of type FILE * and so must be the function that returns it.
FILE *fp_in, *fp_out, *fopen();
The user will be prompted for both the SOURCE and the DESTINATION
file names, and the function gets() will be used to read them!
printf("Enter input file_name\n");
gets(file_in);
printf("Enter output file_name\n");
gets(file_out);
:
}
At this point the SOURCE file will be opened
for READ, and the DESTINATION file for WRITE!
Select the expressions that will open the source file for
reading and the destination file for writing:
a if ((fp_in = fopen(file_in, "r")) != NULL)
if ((fp_out = fopen(file_out, "w")) != NULL) {
b if ((fp_in = fopen(file_in, read)) != NULL)
if ((fp_out = fopen(file_out, write)) != NULL) {
c if ((fp_in = fopen(file_in, "r")) != EOF)
if ((fp_out = fopen(file_out, "w")) != EOF) {
Select one:
That is correct!
You've got it on the 2nd try.
Good!, Now you understand the concept!
Think about the mode specifier!
Think about the value fopen() returns!
Please review Topic 10: Formatted Text I/O in Lesson 2
FORGET5
The correct choice is a . Choice b has incorrect
open mode specifiers and choice c has an incorrect
test for the value that fopen() returns.
The file copying will be done by:
a) while (( c = getc(fp_in)) != EOF)
putc(c, fp_out);
b) for (; (c = getc(fp_in) != EOF) ;)
putc(c, fp_out);
c) do { c = getc(fp_in); putc(c, fp_out);
} while (c != EOF);
d a and b
e b and c
f a and c
e none of the above
Select one:
That is correct!
You've got it on the 2nd try.
Good! Now you understand the concept!
Study the logic of the statements!
No, try one more time!
Please review Topics 2 and 4: Text File Concepts and Operations
FORGET6
The correct choice is d ! Statement c
is wrong because there is no test for EOF before
the character is written into the file!
Finally, the output file is closed, and then the
input file is closed.
if ((fp_in = fopen(file_in, "r")) != NULL)
if ((fp_out = fopen(file_out, "w")) != NULL) {
/* COPY FILE */
while (( c = getc(fp_in)) != EOF)
putc(c, fp_out);
fclose(fp_out);
}
fclose(fp_in);
Note that there is no reason to close either
file if it was not opened correctly!