ENCORE5
PAUSE
SOMECLEAN
ENCORE1
ENCORE
BOX
SOLUTION
Text File Copying Example
GO
Let us solve this example together.
Step 1 is to establish clear goals.
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 binary data handling.
The file copying process may be represented as:
This is the
data file
copied
in this
example
Source File
--------> This is the
--------> data file
--------> copied
--------> in this
--------> example
Destination File
Let's assume that the source file name is data_in and
the destination file is data_out. The names will be used
as character string constants.
Let's also assume that our buffer may hold up to 512 bytes.
Therefore, we set the constant BUFSIZE to 512, and at the
same time we create the skeleton for the program.
#define BUFSIZE 512 /* 1 data block size */
main()
{
:
:
:
}
Required will be the byte counter and the data buffer, i.e.:
int bytes; /* number of bytes counter */
char buf[BUFSIZE]; /* data buffer */
The file_descriptors for the files will be of what type?
Answer here:
That is correct!
You've got it on the 2nd try.
Good!, Now you understand the concept!
It's a whole number!
It is an integer.
Please review the Basic C Data Types.
FORGET1
The correct choice is int
int fd_in, fd_out; /* I/O file descriptors */
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 BINARY read, and the destination
file for BINARY write:
a if ((fp_in = fopen(file_in, "r")) != NULL)
if ((fp_out = fopen(file_out, "w")) != NULL) {
b if ((fd_in = open("data_in", 0)) != -1)
if ((fd_out = creat("data_out", 0644)) != -1) {
c if ((fd_in = open(file_in, 0)) != 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 file descriptors and
the necessary functions!
Think about the value that open() returns.
Please review the Binary I/O Functions.
FORGET5
The correct choice is b. Choices a and c either use
text processing functions or have incorrect file descriptors!
Considering that the number of bytes requested does not always
equal the number of bytes read, the correct statement for the
actual data copying in block increments is:
1 while (( bytes = read(fd_in, buf, BUFSIZE)) > EOF)
putc(c, fp_out);
2 while (( bytes = read(fd_in, buf, BUFSIZE)) > EOF)
write(fd_out, buf, bytes);
3 do { c = getc(fp_in); putc(c, fp_out);
} while (c != EOF);
a) 1 b) 1 and 3 c) 2 d) 1 and 2 e) none of the above
Select one:
That is correct!
You've got it on the 2nd try.
Good! Now you understand the concept!
This is binary file copying.
Only one of the above statements is correct!
Please review Binary I/O Functions!
FORGET6
The correct choice is c! Statements 1 and 3 are incorrect because
they mix character handling functions with binary processing!
Finally, the output file is closed, and then the input file ...
if ((fd_in = open("data_in", 0)) != -1)
if ((fd_out = open("data_out", 0644)) != -1) {
/* COPY FILE */
while (( bytes = read(fd_in, buf, BUFSIZE)) > 0)
write(fd_out, buf, bytes);
putc(c, fp_out);
close(fd_out);
}
close(fd_in);
Note that there is no reason to close either file if it was
not opened correctly!!!