Module5 Files
Module5 Files
1. FILES: A File is a collection of data stored on a secondary storage device like hard
disk.
Till now, we had been processing data that was entered through the computer’s
keyboard. But this task can become very tedious especially when there is a huge
amount of data to be processed.
A better solution, therefor, is to combine all the input data into a file and then
design a C program to read this data from the file whenever required.
If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of the file
using a few commands in C.
You can easily move your data from one computer to another without any changes.
3. STREAMS IN C
In c, the standard streams are termed as pre-connected input and output channels
between a text terminal and the program.
A stream is a logical interface to the devices that are connected to the computer.
Stream is widely used as a logical interface to a file where a file can refer to a disk
file, the computer screen, keyboard, etc.
The 3 standard Streams in C are
1. Standardinput (stdin)
2. Standardoutput (stdout)
3. Standard error (stderr)
Standardinput (stdin): Standard input or the stream from which the program receives
its data. The program requests transfer of data using the real operations. However, not all
programs require input. Generally, unless redirected, input for a program is expected from the
keyboard.
Standarderror(stderr):
Standard error is basically an output stream used by programs to report error messages or
diagnostics. It is a stream independent of standard output and can be redirected separately.
A stream is linked to a file using an open operation and dissociated from a file using a close
operation.
program writes data to buffer Data from the buffer is written to the disk file
PROGRAM BUFFER
DISK
Similarly, when reading data from a dis file, the data is read as a bloc from the file
and written into the buffer.
The program reads data from the buffer. The creation and operation of the buffer
is automatically handled by the operating system.
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
1. Text files
Text files are the normal .txt files. You can easily create text files using any
simple text editors such as Notepad.
When you open those files, you'll see all the contents within the file as plain
text. You can easily edit or delete the contents.
They take minimum effort to maintain, are easily readable, and provide the
least security and takes bigger storage space.
2. Binary files
They can hold a higher amount of data, are not readable easily, and provides
better security than text files.
File Operations
In C, you can perform four major operations on files, either text or binary:
3. Closing a file
FILE *fptr;
ptr = fopen("fileopen","mode");
For example,
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram .
The first function creates a new file named newprogram.txt and opens it for
writing as per the mode 'w'.
The writing mode allows you to create and edit (overwrite) the contents of the
file.
Now let's suppose the second binary file oldprogram.bin exists in the
location E:\cprogram . The second function opens the existing file for reading in
binary mode 'rb'.
The reading mode only allows you to read the file, you cannot write into the
file.
Opening Modes in Standard I/O
Open for both reading and writing in binary If the file does not exist, fopen() returns
rb+
mode. NULL.
a+ Open text file for both reading and appending. If the file does not exist, it will be created
Closing a File
The file (both text and binary) should be closed after reading/writing.
fclose(fptr);
They are just the file versions of printf() and scanf() . The only difference is
that fprintf() and fscanf() expects a pointer to the structure FILE.