C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
}

Here is what MYDATA.DAT now contains (notice the extra line):


Click here to view code image


Here is the collection of books:
#1: 10 Count Trivia by Dean Miller
It is 250 pages and costs $14.99
#2: Moving from C to C++ by Greg Perry
It is 600 pages and costs $39.99
#3: The Stand by Stephen King
It is 1200 pages and costs $24.99

More books to come!

The Absolute Minimum
The goal of this chapter was to show you how to create, read, and write sequential
files. Your C program must open a file before data can be written to or read from the
file. When your program is done with a file, the program should close the file.
When reading from a file, you must check for the end-of-file condition to ensure that
you don’t try to read past the end of the file. The feof() function is a built-in C
function that you use to check for the end of the file. Key concepts from this chapter
include:


  • Store long-term data in data files.

  • Open a file with fopen() before you use the file.

  • Always close a file with fclose() when you’re done.

  • Don’t read from a file without checking for feof() because you might have
    previously read the last line in the file.

  • Don’t use the filename when you open a file. Use the file pointer that you connected
    to the file with fopen().

  • Don’t forget that the file pointer goes at the beginning of fprintf() and that
    fputs() requires a file pointer at the end of its argument list.

Free download pdf