This lab is about reading a file, and displaying the contents. One approach to this is to read in the entire file, then display it. But how do you know that the file will fit in memory? No matter how much memory the computer has, a file could exceed that. Thus, what we will do in this assignment is to read a little of the file at a time, display it, then repeat this. Also, the file may contain characters that are not printable in ASCII, and we will substitute them with printable characters.
abc
12345
then your program should output
1 "abc"
2 "12345"
Your program should process the entire file.
bool printNextLine(char *buffer, char *ptr, int bufferFillLength, int *lineNumber, bool startedLineAlready);
where buffer is the pointer to the beginning of the buffer, ptr is the
pointer to the start of the string to print. Variable bufferFillLength
is the total number of characters in the buffer, which normally is going
to be the size of the buffer, but sometimes will be less than that.
For example, suppose that the buffer size is 200 characters, and the file has 300 characters. The first read will fill the buffer with 200 characters. The second read will fill it with 100 characters, because that is how many are left in the file. So bufferFillLength is 200 the first time, and 100 the second time.
Variable lineNumber is the next line number. That is passed
by reference so that your function can update it. Finally,
startedLineAlready
is used to tell your function when the line has already been started. This
tells it whether to print the next line number, space, and quote before
the line contents (false), or if it should just print the line contents
because it has already printed the line number, space, and quote (true).
If your function prints part of the buffer, but reaches the end of the
buffer, the function should return true. Otherwise, it should return false.
This return value lets the calling function know if it should call it again,
or if it should read from the file again. That is, call your function in
a loop until it has exhausted the buffer, then read more from the file,
and start the loop again. Also the value returned by your function is related
to startedLineAlready;
you should be able to figure out how. Verify that
it gives the same output as in Part 2.
When you are ready to turn this in, remember to follow the directions on turning in labs/homeworks.