11
C File Processing
1
© 2007 Pearson Education, Inc. All rights reserved.
I read part of it all the way through.
Hats off!
The flag is passing by.
2
© 2007 Pearson Education, Inc. All rights reserved.
Consciousness … does not appear to itself chopped up in bits. … A “river” or a “stream” are the metaphors by which it is most naturally described.
I can only assume that a “Do Not File” document is filed in a “Do Not File” file.
3
© 2007 Pearson Education, Inc. All rights reserved.
OBJECTIVES
In this chapter you will learn:
4
© 2007 Pearson Education, Inc. All rights reserved.
11.1 Introduction
11.2 Data Hierarchy
11.3 Files and Streams
11.4 Creating a Sequential-Access File
11.5 Reading Data from a Sequential-Access File
11.6 Random-Access Files
11.7 Creating a Random-Access File
11.8 Writing Data Randomly to a Random-Access File
11.9 Reading Data from a Random-Access File
11.10 Case Study: Transaction-Processing Program
5
© 2007 Pearson Education, Inc. All rights reserved.
11.1 Introduction
6
© 2007 Pearson Education, Inc. All rights reserved.
11.2 Data Hierarchy
Decimal digits, letters, and special symbols
7
© 2007 Pearson Education, Inc. All rights reserved.
11.2 Data Hierarchy
8
© 2007 Pearson Education, Inc. All rights reserved.
9
Fig. 11.1 | Data hierarchy.
© 2007 Pearson Education, Inc. All rights reserved.
11.2 Data Hierarchy
10
© 2007 Pearson Education, Inc. All rights reserved.
11.3 Files and Streams
11
© 2007 Pearson Education, Inc. All rights reserved.
11.3 Files and Streams
12
© 2007 Pearson Education, Inc. All rights reserved.
13
Fig. 11.2 | C’s view of a file of n bytes.
© 2007 Pearson Education, Inc. All rights reserved.
11.3 Files and Streams
14
© 2007 Pearson Education, Inc. All rights reserved.
11.4 Creating a Sequential-Access File
15
© 2007 Pearson Education, Inc. All rights reserved.
11.4 Creating a Sequential-Access File
16
© 2007 Pearson Education, Inc. All rights reserved.
17
Outline
fig11_03.c
(1 of 2 )
FILE pointer definition creates new file pointer
fopen function opens a file; w argument means the file is opened for writing
© 2007 Pearson Education, Inc. All rights reserved.
18
Outline
fig11_03.c
(2 of 2 )
feof returns true when end of file is reached
fprintf writes a string to a file
fclose closes a file
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.1
Opening an existing file for writing ("w") when, in fact, the user wants to preserve the file, discards the contents of the file without warning.
19
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.2
Forgetting to open a file before attempting�to reference it in a program is a logic error.
20
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.3
Using the wrong file pointer to refer to a file is a logic error.
21
© 2007 Pearson Education, Inc. All rights reserved.
Error-Prevention Tip 11.1
Be sure that calls to file processing functions�in a program contain the correct file pointers.
22
© 2007 Pearson Education, Inc. All rights reserved.
23
Fig. 11.4 | End-of-file key combinations for various popular�operating systems.
© 2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 11.1
Explicitly close each file as soon as it is known that the program will not reference the file again.
24
© 2007 Pearson Education, Inc. All rights reserved.
Performance Tip 11.1
Closing a file can free resources for which other users or programs may be waiting.
25
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.4
Opening a nonexistent file for reading is an error.
26
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.5
Opening a file for reading or writing without having been granted the appropriate access rights to the file (this is operating-system dependent) is an error.
27
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.6
Opening a file for writing when no disk space is available is an error.
28
© 2007 Pearson Education, Inc. All rights reserved.
29
Fig. 11.5 | Relationship between FILE pointers, FILE structures and FCBs.
© 2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 11.7
Opening a file with the incorrect file mode is�a logic error. For example, opening a file in write mode ("w") when it should be opened in update mode ("r+") causes the contents of the file to be discarded.
30
© 2007 Pearson Education, Inc. All rights reserved.
31
Fig. 11.6 | File opening modes.
© 2007 Pearson Education, Inc. All rights reserved.
Error-Prevention Tip 11.2
Open a file only for reading (and not update) if the contents of the file should not be modified. This prevents unintentional modification of the file’s contents. This is another example of the principle of least privilege.
32
© 2007 Pearson Education, Inc. All rights reserved.
11.5 Reading Data from a Sequential-Access File
cfPtr = fopen( “clients.dat", "r" );
fscanf( cfPtr, "%d%s%f", &accounnt, name, &balance );
33
© 2007 Pearson Education, Inc. All rights reserved.
34
Outline
fig11_07.c
(1 of 2 )
fopen function opens a file; r argument means the file is opened for reading
© 2007 Pearson Education, Inc. All rights reserved.
35
Outline
fig11_07.c
(2 of 2 )
fscanf function reads a string from a file
© 2007 Pearson Education, Inc. All rights reserved.
36
Outline
fig11_08.c
(1 of 4 )
© 2007 Pearson Education, Inc. All rights reserved.
37
Outline
fig11_08.c
(2 of 4 )
© 2007 Pearson Education, Inc. All rights reserved.
38
Outline
fig11_08.c
(3 of 4 )
© 2007 Pearson Education, Inc. All rights reserved.
39
Outline
fig11_08.c
(4 of 4 )
rewind function moves the file pointer back to the beginning of the file
© 2007 Pearson Education, Inc. All rights reserved.
40
Outline
© 2007 Pearson Education, Inc. All rights reserved.
11.5 Reading Data from a Sequential-Access File
41
© 2007 Pearson Education, Inc. All rights reserved.
11.6 Random-Access Files
42
© 2007 Pearson Education, Inc. All rights reserved.
43
Fig. 11.10 | C’s view of a random-access file.
© 2007 Pearson Education, Inc. All rights reserved.
11.7 Creating a Random-Access File
44
© 2007 Pearson Education, Inc. All rights reserved.
11.7 Creating a Random-Access File
fwrite( &number, sizeof( int ), 1, myPtr );
In this case, "one element" of an array is being transferred
45
© 2007 Pearson Education, Inc. All rights reserved.
11.7 Creating a Random-Access File
fwrite( &myObject, sizeof (struct myStruct), 1, myPtr );
46
© 2007 Pearson Education, Inc. All rights reserved.
47
Outline
fig11_11.c
(1 of 2 )
© 2007 Pearson Education, Inc. All rights reserved.
48
Outline
fig11_11.c
(2 of 2 )
fopen function opens a file; wb argument means the file is opened for writing in binary mode
fwrite transfers bytes into a random-access file
© 2007 Pearson Education, Inc. All rights reserved.
11.8 Writing Data Randomly to a Random-Access File
49
© 2007 Pearson Education, Inc. All rights reserved.
50
Outline
fig11_12.c
(1 of 2 )
© 2007 Pearson Education, Inc. All rights reserved.
51
Outline
fig11_12.c
(2 of 2 )
fseek searches for a specific location in the random-access file
© 2007 Pearson Education, Inc. All rights reserved.
52
Outline
© 2007 Pearson Education, Inc. All rights reserved.
53
Fig. 11.14 | File position pointer indicating an offset of 5 bytes from�the beginning of the file.
© 2007 Pearson Education, Inc. All rights reserved.
11.9 Reading Data from a Random-Access File
fread( &client, sizeof (struct clientData), 1, myPtr );
54
© 2007 Pearson Education, Inc. All rights reserved.
55
Outline
fig11_15.c
(1 of 2 )
© 2007 Pearson Education, Inc. All rights reserved.
56
Outline
fig11_15.c
(2 of 2 )
fread reads bytes from a random-access file to a location in memory
© 2007 Pearson Education, Inc. All rights reserved.
57
Outline
fig11_16.c
(1 of 10 )
© 2007 Pearson Education, Inc. All rights reserved.
58
Outline
fig11_16.c
(2 of 10 )
© 2007 Pearson Education, Inc. All rights reserved.
59
Outline
fig11_16.c
(3 of 10 )
Function textFile creates a text file containing all account data
© 2007 Pearson Education, Inc. All rights reserved.
60
Outline
fig11_16.c
(4 of 10 )
Function updateRecord changes the balance of a specified account
© 2007 Pearson Education, Inc. All rights reserved.
61
Outline
fig11_16.c
(5 of 10 )
© 2007 Pearson Education, Inc. All rights reserved.
62
Outline
fig11_16.c
(6 of 10 )
Function deleteRecord removes an existing account from the file
© 2007 Pearson Education, Inc. All rights reserved.
63
Outline
fig11_16.c
(7 of 10 )
© 2007 Pearson Education, Inc. All rights reserved.
64
Outline
fig11_16.c
(8 of 10 )
Function newRecord adds a new account to the file
© 2007 Pearson Education, Inc. All rights reserved.
65
Outline
fig11_16.c
(9 of 10 )
© 2007 Pearson Education, Inc. All rights reserved.
66
Outline
fig11_16.c
(10 of 10 )
© 2007 Pearson Education, Inc. All rights reserved.