Programming in C
Lecture 8: C File Processing
Outline
Data and Data Files
Files and Streams
Sequential Access Files
Random Access Files
2
Data and Data Files
Data Files can be created, updated, and
processed by C programs
used for permanent storage(持久存储) of large
amounts of data
Storage of data in variables and arrays is only
temporary(临时的)
All such data are lost when the program
terminates
3
The Data Hierarchy - 1
Bit – smallest data item
Value of 0 or 1
Byte – 8 bits
Used to store a character: Decimal digits, letters,
and special symbols
Field – group of characters conveying
meaning
Example: your name
4
The Data Hierarchy - 2
Record – group of related fields
Represented by a struct (or a class in C++)
Example: In a payroll system, a record for a
particular employee that contained his/her
identification number (possible record key), name,
address, etc.
File – group of related records
Example: payroll file
Database – group of related files
5
The Data Hierarchy - 3
6
The Data Hierarchy - 4
Example (Fig. ):
1 Bit
01001010 Byte
Judy Field
Judy Green Record
Sally Black
Judy Blue
Iris Orange
Randy Red
File
7
Outline
Data and Data Files
Files and Streams
Sequential Access Files
Random Access Files
8
Files and Streams
C views each file as a sequence of bytes
File ends with the end-of-file (EOF) marker
Or, file ends at a specified byte
9
EOF and feof() are defined in <>.
Example:
while ((c = getchar()) != EOF) {…}
if (!feof(stdin) {…}
EOF Key Combinations
Different for different OS:
10
Computer system Key combination
UNIX systems <return> <ctrl> d
Windows <ctrl> z
Macintosh <ctrl> d
VAX(VMS) <ctrl> z
Files and Streams
A Stream is created when a file is opened
Provide communication channel(通信渠道)
between files and programs
Opening a file returns a pointer to a FILE structure
Example file pointers:
stdin - standard input (keyboard)
stdout - standard output (screen)
stderr - standard error (screen)
11
FILE structure
*The programmer need not know the specifics
of the FILE structure to use files.
File descriptor(文件描述符)
Index into operating system (OS) array called the
open file table
File Control Block (FCB)
Found in every array element, system uses it to
administer the file
(Refer to Fig. on page 437)
12
Outline
Data and Data Files
Files and Streams
Sequential Access Files
Random Access Files
13
Read/Write Functions - 1
#include <>
int fgetc(FILE *stream)
Read one character from a file
Take a FILE pointer as an argument
fgetc(stdin) equivalent to getchar()
int fputc(int c, FILE *stream)
Write one character to a file
Take a FILE pointer and a character to be written as
arguments
fputc('a', stdout) equivalent to putchar('a')
14
Read/Write functions - 2
char* fgets(char *s, int n, FILE *stream)
Reads a line from a file
int fputs(char *s, FILE *stream)
Writes a line to a file
fscanf / fprintf
int fscanf(FILE *stream, const char * format, …)
int fprintf(FILE *stream, const char * format, …)
File processing equivalents of scanf and printf
15
16
/* Framework for Create a sequential file, see Fig. on P433 for details */
#include <>
int main()
{ ……
FILE *cfPtr; /* cfPtr = file pointer */
if ( ( cfPtr = fopen( "", "w" ) ) == NULL )
printf( "File could not be opened.\n" );
else
{ ……
scanf( "%d%s%lf", &account, name, &balance );
while ( !feof( stdin ) )
{
fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );
}
fclose( cfPtr );
}
return 0;
}
Creating a Sequential Access File
FILE *cfPtr;
Declares a FILE pointer called cfPtr
cfPtr = fopen("", "w");
Function fopen returns a FILE pointer to file
specified
Takes two arguments – the file to be opened and
the open mode
If open fails, NULL is returned
17
Creating a Sequential Access File
fprintf
Used to print to a file
Like printf, except first argument is a FILE pointer
(pointer to the file you want to print )
feof(FILE pointer)
Returns true if end-of-file (EOF) indicator (no more
data to process) is set for the specified file
fclose(FILE pointer)
Closes specified file
Performed automatically when program ends
Good practice: close files explicitly
18
File Open Modes
19
Computer system Key combination
r Open a file for reading.
w Create a file for writing. If the file already exists, discard the current
contents.
a Append; open or create a file for writing at end of file.
r+ Open a file for update (reading and writing); the file must exists.
w+ Create an empty file for both reading and writing. If the file already exists,
discard the current contents.
a+ Open a file for reading and appending; writing is done at the end of the file.
rb Open a file for reading in binary mode.
wb Create a file for writing in binary mode. If the file already exists, discard the
current contents.
ab Append; open or create a file for writing at end of file in binary mode.
rb+ (or r+b) Open a file for update (reading and writing) in binary mode.
wb+ (or w+b) Create a file for update (reading and writing) in binary mode. If the file
already exists, discard the current contents.
ab+ (or a+b) Read and Append; open or create a file for update in binary mode; writing is
done at the end of the file.
Sequential Access Files
Also called text file (文本文件)
Each byte stores an ASCII code, representing a
character
Format of data in a text file is not identical with
its format stored in memory.
.
7,-14 are ints occupying 4 bytes each internally;
but they are of different size(1 and 3 bytes) as text in a file.
20
21
/* Fig. : reading and printing a sequential file*/
#include <>
int main()
{
……
FILE *cfPtr; /* cfPtr = file pointer */
if ( ( cfPtr = fopen( "", "r" ) ) == NULL )
printf( "File could not be opened\n" );
else
{
printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" );
fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
while ( !feof( cfPtr ) )
{
printf( "%-10d%-13s%\n", account, name, balance );
fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
}
fclose( cfPtr );
}
return 0;
}
Reading Data from a Sequential Access File
Create a FILE pointer, link it to the file to read
cfPtr = fopen( “", "r" );
Use fscanf to read from the file
Like scanf, except first argument is a FILE pointer
fscanf( cfPtr, "%d%s%f", &accounnt, name,
&balance );
Data read from beginning to end
22
Reading Data from a Sequential Access File
File position pointer
Indicates number of next byte to be read / written
Not really a pointer, but an integer value (specifies
byte location)
Also called byte offset
rewind( cfPtr )
Repositions file position pointer to beginning of file
(byte 0)
23
Reading Data from a Sequential Access File
Fields can vary in size
Different representation in files and screen than
internal representation
1, 34, -890 are all ints, but have different sizes in file
Cannot be modified without the risk of
destroying other data
(修改文件内容时有可能破坏不该破坏的数据)
24
Reading Data from a Sequential Access File
25
300 White 400 Jones (old data in file)
If we want to change “White” to “Worthington”,
300 White 400 Jones
300 Worthington
300 Worthington
Data gets overwritten
Outline
Data and Data Files
Files and Streams
Sequential Access Files
Random Access Files
26
Random Access Files
Also called binary files(二进制文件)
Format of data in a binary file is identical(同样
的) with its format stored in memory.
byte doesn’t necessarily represent character;
groups of bytes might represent other types of
data, such as integers and floating-point
numbers
Records in binary files have identical length.
27
Random Access Files
Example:
28
00110001 00110010 00110011 00110100 00110101
00000000 00000000 00110000 0011100112345
1 2 3 4 5
ASCII codes of 5 characters
An integer
Random Access Files
Access individual records without searching
through other records
Instant access to records in a file
(对文件记录的随机访问)
Data can be inserted without destroying other
data
Data previously stored can be updated or
deleted without overwriting
29
Random Access Files
C’ view of a random-access file:
30
100
Bytes
100
Bytes
100
Bytes
100
Bytes
100
Bytes
100
Bytes
0 100 200 300 400 500 Byte
Offsets
31
/* Fig. : Creating a randomly accessed file sequentially , with empty structs. */
#include <>
struct clientData {
int acctNum;
char lastName[ 15 ];
char firstName[ 10 ];
double balance;
};
int main()
{
int i;
struct clientData blankClient = { 0, "", "", };
FILE *cfPtr;
if ( ( cfPtr = fopen( "", "w" ) ) == NULL )
printf( "File could not be opened.\n" );
else
{ for ( i = 1; i <= 100; i++ )
fwrite( &blankClient, sizeof( struct clientData ), 1, cfPtr );
fclose( cfPtr );
}
return 0;
}
Unformatted File I/O Functions
fwrite - Transfer bytes from a location in
memory to a file
size_t fwrite(const void * buffer, size_t size, size_t
nmemb, FILE * fp);
fread - Transfer bytes from a file to a location
in memory
size_t fread(void * buffer, size_t size, size_t
nmemb, FILE * fp);
32
Unformatted File I/O Functions
Example:
fwrite( &number, sizeof( int ), 1, myPtr );
Number --- an integer variable
&number --- location to transfer bytes from
sizeof( int ) --- number of bytes to transfer
1 --- for arrays, number of elements to transfer
In this case, "one element" of an array, . one
number is being transferred
myPtr - File to transfer to
fread similar
33
Unformatted File I/O Functions
fwrite( &myObject, sizeof (struct myStruct), 1,
myPtr );
To write a data block with designated size to a file
sizeof – return the size in bytes of the object in
parentheses
To write several array elements
Pointer to array as first argument
Number of elements to write as third argument
34
35
/* Fig. : Writing to a random access file*/
….
int main()
{ FILE *cfPtr;
struct clientData client = { 0, "", "", };
if ( ( cfPtr = fopen( "", "r+" ) ) == NULL )
printf( "File could not be opened.\n" );
else
{ ……
while ( != 0 )
{ printf( "Enter lastname, firstname, balance\n? " );
fscanf( stdin, "%s%s%lf", , , \
& );
fseek( cfPtr, ( - 1 ) * \
sizeof( struct clientData ), SEEK_SET );
fwrite( &client, sizeof( struct clientData ), 1, cfPtr );
printf( "Enter account number\n? " );
scanf( "%d", & );
}
fclose( cfPtr );
}
return 0;
}
Writing Data Randomly to a Random
Access File
int fseek( FILE *stream, long int offset, int
whence);
Set the file position pointer to a specific position
stream - pointer to file
offset - file position pointer (0 is first location)
whence - specifies where in file we are reading
from
SEEK_SET - seek starts at beginning of file
SEEK_CUR - seek starts at current location in file
SEEK_END - seek starts at end of file
Succeed: return 0
36
Writing Data Randomly to a Random
Access File
37
/*Fig. Reading a random access file sequentially */
……
int main()
{ FILE *cfPtr;
struct clientData client;
if ( ( cfPtr = fopen( "", "r" ) ) == NULL )
printf( "File could not be opened.\n" );
else
{ printf( "%-6s%-16s%-11s%10s\n", \
"Acct", "Last Name", "First Name", "Balance" );
while ( fread( &client, sizeof( struct clientData ), 1, cfPtr ) )
{
if ( != 0 )
printf( "%-6d%-16s%-11s%\n", \
, , \
, );
}
fclose( cfPtr );
}
return 0;
}
Reading Data Sequentially from a
Random Access File
int fread( &client, sizeof (struct clientData), 1,
myPtr );
Reads a specified number of bytes from a file into
memory
Can read several fixed-size array elements
Provide pointer to array
Indicate number of elements to read
Number specified in the 3rd argument
return the number of items successfully read
38
Summary
本章重要内容:
C语言中文件的分类
顺序文件(文本文件)的读写操作
随机文件(二进制文件)的读写操作
两类文件的异同
39
A few words on
C Preprocessing
40
Introduction
Handle by the preprocessor
Not the C complier!
Processed prior to
compilation
编译前进行
All headed with #
井号开头
Procedures to develop a C program 41
#define: Symbolic Constants
Example:
#define PI
replacement-list is any sequence of C tokens
it may include identifiers, keywords, numbers,
character constants, string literals, operators, and
punctuation.
General Form:
#define identifier replacement-list
42
#define: Symbolic Constants
General Form:
#define identifier replacement-list
#define YES 1
#define NO 0
#define PI
#define OUT printf(“Hello,World”);
#define N = 100
#define N 100;
#define PI ;
43
What will N
be replaced
by?
Symbolic Constants Advantages
It makes programs easier to read
It makes programs easier to modify
It helps avoid inconsistencies and
typographical errors
44