Lecture 20: C File Processing Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program.

Download Report

Transcript Lecture 20: C File Processing Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program.

Lecture 20: C File Processing
Why Using Files?
Storage of data in variables and arrays is temporary
Data lost when a program terminates.
Files are used for permanent retention of data
Stored on secondary storage devices
Disks
Optical disks (CDs, DVDs)
USB memory key
C Files and Streams
C views each file as a sequence of bytes
File ends with the end-of-file marker
Stream created when a file is opened
Provide communication channel between files and
programs
Opening a file returns a pointer to a FILE structure
Defined in <stdio.h>
Contains information used to process the file.
Three files and their associated streams are
automatically opened when program execution begins.
The standard input -- stdin (keyboard)
The standard output -- stdout (keyboard)
The standard error -- stderr (screen)
Opening a File for Sequential-Access
Opening a file for reading
Define a file pointer
FILE *rfPtr;
Defines a FILE pointer called rfPtr;
Open the file
rfPtr = fopen(“sData.txt”, “r”);
Function fopen returns a FILE pointer to the file
specified; link it to the file to read
Takes two arguments
Filename -- file to open
File open mode -- “r”
Open an existing file for reading.
If open fails, NULL returned
Opening a File for Sequential-Access
Using fscanf to read data from the file
fscanf(rfPtr, “%f”, buffer);
Like scanf, except first argument is a FILE pointer
(pointer to the file you want to read from)
Data read from beginning to end
Checking the end of the file
feof(rfPtr)
Returns true if end-of-file indicator (no more data to
process) is set for the specified file.
Closing the file
fclose(rfPtr);
Closes the specified file
Performed automatically when program ends
Good practice to close file explicitly
Creating a Sequential-Access File
Creating a file for writing
Define a file pointer
FILE *wfPtr;
Creates a FILE pointer called wfPtr;
Create the file
wfPtr = fopen(“cData.txt”, “w”);
Function fopen returns a FILE pointer to the file
specified
Takes two arguments
Filename -- file to open
File open mode -- “w”
Create a file for writing. If the file already exists, discard
the current contents.
If open fails, NULL returned
Creating a Sequential-Access File
Writing data to the file
fprintf(wfPtr, “%.3f ”, buffer);
Like printf, except first argument is a FILE pointer
(pointer to the file you want to print in)
Closing the file
fclose(wfPtr);
Closes the spedified file
Performed automatically when program ends
Good practice to close file explicitly
File Opening Modes
Mode Description
r
Open an existing file for reading.
w
a
Create a file for w riting. If the file already exists, discard the current
contents.
Append; open or create a file for writing at the end of the file.
r+
Open an existing file for update (reading and writing).
w+
Create a file for update. If the file already exists, discard the current
contents.
Append: open or create a file for update; writing is done at the end
of the file.
Open an existing file for reading in binary mode.
Create a file for w riting in binary mode. If the file already exists,
discard the current contents.
Append; open or create a file for writing at the end of the file in
binary mode.
Open an existing file for update (reading and writing) in binary
mode.
Create a file for update in binary mode. If the file already exists,
discard the current contents.
Append: open or create a file for update in binary mode; writing is
done at the end of the file.
a+
rb
wb
ab
rb+
wb+
ab+
Other Issues
Programs may process no files, one file, or many files.
Each file must have a unique name and should have its
own pointer.
Formatting Output
printf( ), sprintf( ), and fprintf( ).
Conversion
Description
specifier
%d
Display as a signed decimal integer.
%i
Display as a signed decimal integer. [Note: T he i and d specifiers are diffe rent
when used with scanf.]
%o
Display as an unsigned octal integer.
%u
Display as an unsigned decimal integer.
%x or %X
Display as an unsigned hexadecimal integer. X causes the digits 0-9 and the
letters A-F to be displayed and x causes the digits 0-9 and a-f to be displayed.
h or l (letter l)
Place before any integer conversion specifier to indicate that a short or long
integer is displayed,respectively. Letters h and l are more precisely called length
modifiers.
%e or %E
Display a flo ating-point value in expo nential notation.
%f
Display floating-point values in fixed-point notation .
%g or %G
Display a flo ating-point value in either the flo ating-point form f or the
expo nen tial form e (or E), based on the magnitude of the value.
L
Place before an y floating-point con version specifi er to indicate that a long
double floating-point value is displayed.
Formatting Output
printf( ), sprintf( ), and fprintf( ).
Field width
The exact size of a field in which data is printed
An integer representing the field width is inserted
between the percent sign (%) and the conversion
specifier. Ex. %6d
Field widths can be used with all conversion
specifiers
Precision for floating-point numbers
The number of digits to appear after the decimal
point. Ex. %6.2f
When printed with a precision smaller than the
original number of decimal places in the value, the
value is rounded.
Formatting Input
scanf( ),
sscanf( ),
and
fscanf( ).
Conversi
on
Description
specifier
Integers
%d
Read an optionally signed decimal integer. The corresponding argument is a
pointer to an int.
%i
Read an optionally signed decimal, octal or hexadecimal
integer. T he corresponding argument is a pointer to an int.
%o
Read an octal integer. T he corresponding argument is a pointer to an unsigned int.
%u
Read an unsigned decimal integer. T he corresponding argument is a pointer to an
unsigned int.
%x or %X
Read a hexadecimal integer. T he corresponding argument is a pointer to an
unsigned int.
h or l
Place before any of the integer conversion specifiers to indicate that a short or
long integer is to be input.
Floating-point numbers
%e, %E, %f,
%g or %G
Read a floating-point value. T he corresponding argument is a pointer to a floatingpoint variable.
l or L
Place before any of the floating-point conversion specifiers to indicate that a
double or long double value is to be input. T he corresponding argument is a pointer
to a double or long double variable.
Characters and strings
%c
Read a character. T he corresponding argument is a pointer to
a char ; no null ('\0') is added.
%s
Read a string. T he corresponding argument is a pointer to an array of type char
that is large enough to hold the string and
a terminating null ('\0') characterÑ which is automatically added.