Ch. 13: File Processing

Ch. 13: File Processing - File Processing Outline Files and...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
File Processing Outline Files and Streams Open and Close Files Read and Write Sequential Files Read and Write Random Access Files Read and Write Random Access Files with Structures Obtaining Information about Files and Directories
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Files and Streams •C f i l e s C views each file simply as a sequential stream of bytes as shown below. It ends as if there was an end-of-file marker. 0 1 2 .
Background image of page 2
•S t r e am – Provide communication channel between files and programs – Stream created when a file is opened Function feof() tests the end-of-file for a stream.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Open and Close Files A file is the most common I/O facility that can be used as a stream. Data type FILE , defined in stdio.h , maintains information about the stream. An object of type FILE * , created by calling a function such as fopen() , is used to access the file by other file manipulation functions such as fprintf () and fscanf() . Function fopen() is prototyped as FILE *fopen(const char *filename, const char *mode); filename is the name of the file which will be opened and associated with a stream. Argument mode specifies the meaning of opening a file. The valid values for this argument are described in the following table.
Background image of page 4
The valid values for mode argument. Mode Meaning r Open a text file for reading. w Truncate to zero length or create a text file for writing. a Append; open or create a text file for writing at the end-of-file . rb Open a binary file for reading. wb Truncate to zero length or create a binary file for writing. ab Append; open or create a binary file for writing at the end-of-file. r+ Open a text file for read/write. w+ Truncate to zero length or create a text file for read/write. a+ Append; open or create a text file for read/write. You can read data anywhere in the file, but you can write data only at the end-of-file. r+b or rb+ Open a binary file for read/write. w+b or wb+ Truncate to zero length or create a binary file for read/write. a+b or ab+ Append; open or create a binary file for read/write. You can read data anywhere in the file, but you can write data only at the end-of-file.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
If file cannot be opened, function fopen () returns NULL. All files which are opened and associated with streams should be closed before a program terminates. Function fclose() shall be used to close a file opened by function fopen (). It is prototyped as int fclose(FILE *stream); fclose() causes the stream pointed to by stream to be flushed and the associated file to be closed. It returns 0 when successful. Otherwise, it returns non-zero value.
Background image of page 6
Example : #include <stdio.h> FILE *fpt1, *fpt2; /* create file named “filename" */ if((fpt1 = fopen(“filename",“w")) == NULL) {
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

Ch. 13: File Processing - File Processing Outline Files and...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online