Reading a file

Part 1
Page 547 of your C textbook has a program that checks to see if a file can be opened for reading. Implement it, that is, type the program (or get it from the book's website), compile it, and run it a few times with different input. It's only 13 lines, and typing it out yourself should help you understand it.

Part 2
Make a copy of the program from part 1, calling it lab9_part2.c or something equally good. Then add code to it to read the file using fgets, and write it to standard output with double-quotes around it, along with the line number and a space before it. Assume that your input file has character data in it. For example, if the input file has the contents:
abc
12345
then your program should output
1 "abc"
2 "12345"
Your program should process the entire file.

Part 3
Make a copy of the program from part 2, calling it lab9_part3.c or something equally good. In this version, use fread instead of fgets. Choose a reasonable buffer size. Instead of the null character ending the strings, you will need to look for the line-feed character (10) instead. This means that you will need to process the input more carefully, so create a function to print the next line, given a char pointer. It will also need to know the number of characters in the buffer, and where the buffer begins. Here is the function prototype:


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 prints. 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.

Part 4
Make a copy of the program from part 3, calling it lab9_part4.c or something equally good. In this version, instead of printing every character, have your function print a comma in place of a non-text character. That is, most characters 0 to 31 are not meant to be part of a text file. There are a few exceptions, like line-feed and tab. Find a good ASCII chart to see what values these have. Also, characters above 127 are not considered text either, and may appear differently depending on the computer. So allow any character from 32 to 127 to be printed normally, print a comma in place of any character above 127, and print a comma for any value below 32 except for the tab and line-feed. Verify that it works as expected.

When you are ready to turn this in, remember to follow the directions on turning in labs/homeworks.