Module 5 Files
Module 5 Files
Introduction to Files
A File is a collection of data stored in the secondary memory. So far data was entered
into the programs through the keyboard. So Files are used for storing information that can be
processed by the programs. Files are not only used for storing the data, programs are also stored
in files. In order to use files, we have to learn file input and output operations. That is, how data
is read and how to write into a file.
A Stream is the important concept in C. The Stream is a common, logical interface to the
various devices that comprise the computer. So a Stream is a logical interface to a file. There are
two types of Streams, Text Stream and Binary Stream. A Text File can be thought of as a stream
of characters that can be processed sequentially. It can only be processed in the forward
direction.
Using Files in C
To use a file four essential actions should be carried out. These are,
a. Declare a file pointer variable.
b. Open a file using the fopen() function.
c. Process the file using suitable functions.
d. Close the file using the fclose()
Declaration of file pointer:
A pointer variable is used to points a structure FILE. The members of the FILE structure are
used by the program in various file access operation, but programmers do not need to concerned
about them.
FILE *file_pointer_name;
Eg : FILE *fp;
Opening a file
To open a file using the fopen() function. Its syntax is,
FILE *fopen(const char *fname,const char* mode);
const char *fname represents the file name. The file name is like “D:\\501\example.txt”.
Here D: is a drive, 501 is the directory, example.txt is a file name.
const char *mode represents the mode of the file. It has the following values.
Mode Description
R Opens a text file in reading mode
W Opens or create a text file in writing mode
A Opens a text file in append mode
r+ Opens a text file in both reading and writing mode
w+ Opens a text file in both reading and writing mode
a+ Opens a binary file in reading mode
Rb Opens a binary file in reading mode
Wb Opens or create a binary file in writing mode
Ab Opens a binary file in append mode
rb+ Opens a binary file in both reading and writing mode
wb+ Opens a binary file in both reading and writing mode
ab+ Opens a binary file in both reading and writing mode
Difference between write mode and append mode is,
Write (w) mode and Append (a) mode, while opening a file are almost the same. Both are
used to write in a file. In both the modes, new file is created if it doesn't exists already.
The only difference they have is, when you open a file in the write mode, the file is reset,
resulting in deletion of any data already present in the file. While in append mode this will not
happen. Append mode is used to append or add data to the existing data of file(if any). Hence, when
you open a file in Append(a) mode, the cursor is positioned at the end of the present data in the file.
fgetc()
fread()
fputs()
fputc()
fwrite()
/* read and write the content of the file using fprintf() and fscanf() functions */
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
clrscr();
/* open for writing */
fptr = fopen("abc.txt", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %f\n", salary);
getch();
fclose(fptr);
}