Beruflich Dokumente
Kultur Dokumente
UT D
Introduction
File Processing
Introduction
File processing in C is a big subject, and unfortunately we
dont have time to cover more than the basics right now.
In C, we can read and write sequential files (i.e., files
that are written to or read from in a sequential manner,
generally text files, which may be interpreted according
to the ASCII table), and random access files (which are
generally considered as collections of raw bytes). There
are several functions that support both of these.
File Processing
UT D
Opening a File
Book 11.3.1, 11.3.2
File Processing
Files and Streams
File Processing
Using a File File Structures
File Processing
Opening a File
Call the fopen() function and assign the return value to that FILE
stream pointer.
That FILE stream can then be used with any of the I/O
functions.
10
File Processing
Opening a File
The fopen() function prototype:
FILE* fopen(const char* filename, const char* mode);
File Processing
Opening a File
The fopen() function prototype:
FILE* fopen(const char* filename, const char* mode);
A more common way to open a file would include a test of the return
value from fopen() to see if it is NULL. If it is, then there has been
an error opening the file:
#include <stdio.h>
FILE * cfPtr;
if ( (cfPtr = fopen (filename.txt, w)) == NULL )
printf (There has been a file error.);
else
{
// proceed with normal processing
}
12
File Processing
Using a File Opening mode
21
File Processing
Using a File Opening mode
A plus sign may be added to the letter (+) in the opening mode
argument. This indicates that the file may be updated, no matter
what its opening mode was:
r+
w+
a+
File Processing
Using a File Opening mode
UT D
Using a File
Book 11.3.4, 11.4
31
The program prompts the user to enter the fields for each
record or to enter end-of-file (EOF) when data entry is
complete.
Figure 11.3 lists the key combinations for entering end-offile for various computer systems.
Function feof to determine whether the end-of-file
indicator is set for the file to which stdin refers.
The end-of-file indicator informs the program that theres no more
data to be processed.
File Processing
Using a File
Remember that scanf() and fscanf() will tokenize
around white spaces.
It is frequently useful to be able to read (write) whole lines
from (to) a file. The following two functions work for that
purpose:
fgets()
fputs()
36
File Processing
Using a File
fgets()
char * fgets (char * strPtr, int size, FILE
*cfPtr);
File Processing
Using a File
fputs()
int fputs (const char * strPtr, FILE * cfPtr);
The function fputs() will write the string strPtr to the file
fp.
38
File Processing
Files and Streams
39
File Processing
Files and Streams
40
File Processing
Input/Output
File stream
Standard stream
Formatted read
int fscanf(FILE *
cfPtr, const char*
format, ...)
int scanf(const
char *format,
...)
Formatted write
int fprintf(FILE
*cfPtr, const char
*format, ...)
int printf(const
char *format,
...)
Read a line
char *gets(char
*strPtr)
Write a line
int puts(const
char *strPtr)
Read a char
int fgetc(FILE
*cfPtr)
int getchar(void)
Write a char
int putchar(int
char)
41
The statement
rewind( cfPtr );
File Processing
Using a File
The file position index can be changed, even with text files, by
using the following function:
UT D
Closing a File
Book 11.3.5