0% found this document useful (0 votes)
99 views44 pages

File Processing (Student)

Uploaded by

Yap Uen Hsieh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views44 pages

File Processing (Student)

Uploaded by

Yap Uen Hsieh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

 Storage of data in variables and arrays is temporary—


such data is lost when a program terminates.
 Files are used for permanent retention of data.
 We both consider sequential-access and random-access
file processing.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 C views each file simply as a sequential stream of bytes.
 Each file ends either with an _______________or at a
specific byte number recorded in a system-maintained,
administrative data structure.
 When a file is opened, a ___________ is associated with it.
 Three files and their associated streams are automatically
opened when program execution begins—the standard
________, the standard _________ and the standard
________.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 The standard library provides many functions for reading data
from files and for writing data to files.
 Function fgetc, like getchar, reads one character from a file.
 Function fgetc receives as an argument a FILE pointer for the
file from which a character will be read.
 The call fgetc( stdin ) reads one character from stdin—
the standard input.
 This call is equivalent to the call getchar().
 Function fputc, like putchar, writes one character to a file.
 Function fputc receives as arguments a character to be written
and a pointer for the file to which the character will be written.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 The function call fputc( 'a', stdout ) writes the
character 'a' to stdout—the standard output.
 This call is equivalent to putchar( 'a' ).
 Several other functions used to read data from standard
input and write data to standard output have similarly
named file-processing functions.
 The fgets and fputs functions, for example, can be used to
read a line from a file and write a line to a file, respectively.
 In the next several sections, we introduce the file-
processing equivalents of functions scanf and printf—
fscanf and fprintf.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
 The program displays a menu and allows the credit
manager to enter one of three options to obtain credit
information.
 Option 1 produces a list of accounts with zero balances.
 Option 2 produces a list of accounts with credit
balances.
 Option 3 produces a list of accounts with debit
balances.
 Option 4 terminates program execution.
 A sample output is shown in Fig. 11.8.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
 Function fwrite transfers a specified number of
bytes beginning at a specified location in memory to a
file.
 The data is written beginning at the location in the file
indicated by the file position pointer.
 Function fread transfers a specified number of bytes
from the location in the file specified by the file
position pointer to an area in memory beginning with a
specified address.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 Now, when writing an integer, instead of using
 fprintf( fPtr, "%d", number );
which could print a single digit or as many as 11 digits
(10 digits plus a sign, each of which requires 1 byte of
storage) for a four-byte integer, we can use
 fwrite( &number, sizeof( int ), 1, fPtr );
which always writes four bytes on a system with four-
byte integers from a variable number to the file
represented by fPtr (we’ll explain the 1 argument
shortly).

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 Later, fread can be used to read those four bytes into
an integer variable number.
 Although fread and fwrite read and write data,
such as integers, in fixed-size rather than variable-size
format, the data they handle are processed in computer
“raw data” format (i.e., bytes of data) rather than in
printf’s and scanf’s human-readable text format.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 Functions fwrite and fread are capable of reading and
writing arrays of data to and from disk.
 The third argument of both fread and fwrite is the
number of elements in the array that should be read from or
written to disk.
 The preceding fwrite function call writes a single integer
to disk, so the third argument is 1 (as if one element of an
array is being written).
 File-processing programs rarely write a single field to a file.
 Normally, they write one struct at a time, as we show in
the following examples.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
 Consider the following problem statement:
◦ Create a credit-processing system capable of storing up to 100
fixed-length records. Each record should consist of an account
number that will be used as the record key, a last name, a first
name and a balance. The resulting program should be able to
update an account, insert a new account record, delete an
account and list all the account records in a formatted text file
for printing. Use a random-access file.

©1992-2013 by Pearson Education, Inc.


All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.
©1992-2013 by Pearson Education, Inc.
All Rights Reserved.

You might also like