[go: up one dir, main page]

0% found this document useful (0 votes)
13 views28 pages

HG JH

The document discusses file handling in C programming. It covers topics like text files versus binary files, opening and closing files, reading and writing data to files, and random access files. The key steps in processing a file are to create a stream pointer using fopen(), open the file, read or write data using functions like fread() and fwrite(), and close the file with fclose(). Random access files allow direct access to records without searching through other records using functions like fseek() to move to a specific location.

Uploaded by

Akash Raut
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)
13 views28 pages

HG JH

The document discusses file handling in C programming. It covers topics like text files versus binary files, opening and closing files, reading and writing data to files, and random access files. The key steps in processing a file are to create a stream pointer using fopen(), open the file, read or write data using functions like fread() and fwrite(), and close the file with fclose(). Random access files allow direct access to records without searching through other records using functions like fseek() to move to a specific location.

Uploaded by

Akash Raut
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/ 28

C Programming for

Engineers

File Handling
ICEN 360– Spring 2017
Prof. Dola Saha
1
Files in C
Ø 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.
Ø Computers store files on secondary storage devices, such
as hard drives, CDs, DVDs and flash drives.
Ø Objective: how data files are created, updated and
processed by C programs.
Ø We both consider sequential-access and random-access
file processing.

2
Files and Streams
Ø C views each file simply as a sequential stream of bytes.
Ø Each file ends either with an end-of-file marker or at a
specific byte number recorded in a system-maintained,
administrative data structure.
Ø When a file is opened, a stream is associated with it.
Ø Three files and their associated streams are automatically
opened when program execution begins—the standard input,
the standard output and the standard error.
Ø Streams provide communication channels between files and
programs.

3
Text file vs Binary files
Ø Text file is a term used for a file that is essentially a
sequence of character codes.

Ø Binary file is a term used for a file in which most bytes


are not intended to be interpreted as character codes.
Here are a few common binary file formats:
§ PDF, for documents
§ JPEG, GIF, and PNG, for images
§ MP3, for audio tracks

4
Steps in processing a file
Ø Create the stream via a pointer variable using the FILE
structure:
FILE *p;
Ø Open the file, associating the stream name with the file
name.
Ø Read or write the data.
Ø Close the file.

5
Open the file: fopen()
Ø FILE *fopen(const char *filename,
const char *mode);
Mode Purpose Stream Position
r Read Beginning of file
File exists
r+ Read and write Beginning of file
File exists
w Write Beginning of file
If file exists, it is truncated to NULL, otherwise new created.
w+ Write and read Beginning of file
If file exists, it is truncated to NULL, otherwise new created.
a Append (write at end) End of file
File exists
a+ Read and append End of file
File exists
6
Opening Binary Files

7
Functions to read and write data to file
Ø Function fgetc
§ like getchar, reads one character from a file.
§ 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.

Ø Function fputc,
§ like putchar, writes one character to a file.
§ receives as arguments a character to be written and a pointer for
the file to which the character will be written.

8
Functions to read and write data to file
Ø Function fgets
§ Reads one line from a file.
§ char *fgets(char *str, int n, FILE
*stream)

Ø Function fputs
§ Writes one line to a file.
§ int fputs(const char *str, FILE
*stream)

9
Functions to read and write data to file
Ø Function fprintf
§ Like printf
§ Takes first argument as file pointer

Ø Function fscanf
§ Like scanf
§ Takes first argument as file pointer

10
Close the File: fclose()
Ø int fclose(FILE * stream)
Ø Returns 0 if successfully closed
Ø If function fclose is not called explicitly, the operating
system normally will close the file when program
execution terminates.

11
Create a sequential file … (1)

12
Create a sequential file … (2)

13
Read a record from File

14
15
FILE Pointer
Operating System’s
File Control Block

16
Classroom Assignment
Ø Read a text file and copy it to another text file.
§ Sample file: Course webpage

17
Read a record from file…. (1)

18
Read a record from file…. (1)

19
Reset a file position pointer
Ø The statement
o rewind(cfPtr);
causes a program’s file position pointer—which indicates the number
of the next byte in the file to be read or written—to be repositioned
to the beginning of the file (i.e., byte 0) pointed to by cfPtr.
Ø The file position pointer is not really a pointer.
Ø Rather it’s an integer value that specifies the byte in the file at which
the next read or write is to occur.
Ø This is sometimes referred to as the file offset.
Ø The file position pointer is a member of the FILE structure associated
with each file.

20
Random Access File
Ø Individual records of a random-access file are normally
fixed in length and may be accessed directly (and thus
quickly) without searching through other records.
Ø Random-access files are appropriate for
§ airline reservation systems, banking systems, point-of-sale systems,
and other kinds of transaction-processing systems that require rapid
access to specific data.

21
Random Access File
Ø Fixed-length records enable data to be inserted in a
random-access file without destroying other data in the
file.
Ø Data stored previously can also be updated or deleted
without rewriting the entire file.

22
fwrite()
Ø Example use
o fprintf(fPtr, "%d", number);
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
o 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. 1 denotes one integer will be
written.
23
fread()
Ø Function fread reads a specified number of bytes from
a file into memory.
Ø For example,
o fread(&client, sizeof(struct clientData),
1, cfPtr);
reads the number of bytes determined by
sizeof(struct clientData) from the file
referenced by cfPtr, stores the data in client and
returns the number of bytes read.
Ø The bytes are read from the location specified by the file
position pointer.
24
Random Access View

25
Moving to a location
Ø fseek
oint fseek(FILE *stream, long int offset,
int whence);
§ offset is the number of bytes to seek from
§ whence in the file pointed to by stream—a positive offset seeks
forward and a negative one seeks backward.
Ø Argument whence is one of the values
§ SEEK_SET: Value 0, beginning of file.
§ SEEK_CUR: Value 1, current position.
§ SEEK_END: Value 2, end of file.

26
Random Access File Code

27
Random Access File Code

28

You might also like