07-03-2011, 02:52 PM
14575_File Handling.ppt (Size: 831 KB / Downloads: 103)
File Handling in C
Goals
By the end of this unit you should understand …
… how to open a file to write to it.
… how to open a file to read from it.
… how to open a file to append data to it.
… how to read strings from a file.
… how to write strings to a file.
What is a File?
A file is a collection of related data that a computers treats as a single unit.
Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down.
When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.
Buffers
A buffer is a “special work area” that holds data as the computer transfers them to/from memory.
Buffers help to synchronize data the physical devices with the program.
The physical requirements of the devices can deliver more data for input than a program can use at any one time. The buffer handles the overflow data until a program can use it.
Moreover, the buffer also holds data until it is efficient to write that data to the storage device for output.
File Information Table
A program requires several pieces of information about a file, including the name the OS uses for it, the position of the current character, etc.
C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
Streams
In C, we input/output data using streams. We can associate a stream with a device (i.e. the terminal) or with a file.
C supports two types of files
Text Stream Files
Binary Stream Files
Text Streams & Binary Streams
Text streams consist of sequential characters divided into lines. Each line terminates with the newline character (\n).
Binary streams consist of data values such as integers, floats or complex data types, “using their memory representation.”
Today, we’ll concentrate solely on text streams …
Files & Streams
A file is an “independent entity” with a name recorded by the operating system.
A stream is created by a program.
To work with a file, we must associate our stream name with the file name recorded by the OS.
Steps in Processing a File
1. Create the stream via a pointer variable using the FILE structure:
FILE* spData;
2. Open the file, associating the stream name with the file name.
3. Read or write the data.
4. Close the file.
System-Created Streams
C automatically creates three streams that it opens and closes automatically for us in order to communicate with the terminal:
stdin
stdout
stderr
We cannot re-declare these streams in our programs.
Standard I/O Functions in C
File Open
The file open function (fopen) serves two purposes:
It makes the connection between the physical file and the stream.
It creates “a program file structure to store the information” C needs to process the file.
Syntax:
fopen(“filename”, “mode”);
More On fopen
The file mode tells C how the program will use the file.
The filename indicates the system name and location for the file.
We assign the return value of fopen to our pointer variable:
spData = fopen(“MYFILE.DAT”, “w”);
spData = fopen(“A:\MYFILE.DAT”, “w”);
More On fopen
File Open Modes
More on File Open Modes
Reading from a File
Example
Coping the content of one file to another
Writing to a File
Example