ENCORE5
PAUSE
ENCORE1
ENCORE
BOX
SOLUTION
GO
Let's examine this problem 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 binary data handling.
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
Let's assume that the source file name is data_in and
the destination file data_out, which will be used
as character string constants.
Let us assume that our buffer may hold up to 512 bytes
(1 blocksize). 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 above:
int bytes; /* number of bytes counter */
char buf[BUFSIZE]; /* data buffer */
The file_descriptors for the files will be of 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 Lesson 2, Topic 4: 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 open() returns!
Please review the Topic 2: Using 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:
a while (( bytes = read(fd_in, buf, BUFSIZE)) > EOF)
putc(c, fp_out);
b while (( bytes = read(fd_in, buf, BUFSIZE)) > EOF)
write(fd_out, buf, bytes);
c do { c = getc(fp_in); putc(c, fp_out);
} while (c != EOF);
d a and c
e a and b
f 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 Topic 2: Using Binary I/O Functions
FORGET6
The correct choice is b . Statements a and c are wrong 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!