File Handling:
File is a collection of bytes that is
stored on secondary storage devices like disk. There are two kinds of files in
a system.
1.
Text files (ASCII)
2.
Binary files
·
Text files contain ASCII codes
of digits, alphabetic and symbols.
·
Binary file contains collection
of bytes (0’s and 1’s). Binary files are compiled version of text files.
Basic file operations in C
programming:
There are 4 basic
operations that can be performed on any files in C programming language. They
are,
1.
Opening/Creating a file
2.
Closing a file
3.
Reading a file
4.
Writing in a file
fopen() –
fopen()
function is used to open a file to perform operations such as reading, writing
etc. In a C program, we declare a file pointer and use fopen() as below.
fopen() function creates a new file if the mentioned file name does not exist.
FILE *fp;
fp=fopen (“filename”, ”‘mode”);
fp=fopen (“filename”, ”‘mode”);
Where,
fp – file pointer to the data type “FILE”.
Filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on the file. Example: r, w, a etc.
fp – file pointer to the data type “FILE”.
Filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on the file. Example: r, w, a etc.
fclose() -
fclose() function closes the file that is
being pointed by file pointer fp. In a C program, we close a file as
below.
fclose (fp);
fclose (fp);
fprintf() -
The
fprintf() function is same as printf() but instead of writing data to the
console, it writes formatted data into the file. Almost all the arguments of
fprintf() function is same as printf() function except it has an additional
argument which is a file pointer to the file where the formatted output will be
written.
Syntax:-
fprintf(filepointer,
data to be write on file)
Example:-
fprintf(fp, "this is
my file");
fscanf() -
The fscanf() function is used to read
formatted input from the file. It works just like scanf() function. The main
difference is that fscanf() function requires a file pointer.
Syntax:-
fscanf(file
pointer, data to be read from file)
Example:-
fscanf(fp,
"%s", sname);
Different File modes
Mode
|
Description
|
r
|
opens a text file in read mode
|
w
|
opens a text file in write mode
|
a
|
opens a text file in append mode
|
r+
|
opens a text file in read and write
mode
|
w+
|
opens a text file in read and write
mode
|
a+
|
opens a text file in read and write
mode
|
Example 1 (Writing Data in File ):
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
fp=fopen("myfile.txt","w");//opening file
fprintf(fp,"This is my file");//writing data into file
fclose(fp);//closing file
getch();
}
Example 2 (Reading Data from File ):
#include <stdio.h>
#include <conio.h>
main()
{
FILE *fp;
char
data[50];//creating char array to store data of file
fp
= fopen("myfile.txt", "r");
while(fscanf(fp, "%s", data)!=EOF)
{
printf("%s ", data );
}
fclose(fp);
getch();
}
Example 3 (Write operation on File ):
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fp;
int
eid;
char ename[30];
float salary;
fp
= fopen("emp.txt", "w");/*
open for writing */
printf("Enter the id\n");
scanf("%d", &eid);
printf("Enter the name \n");
scanf("%s", &ename);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fp, "%d %s %f", eid, ename, salary);
fclose(fp);
getch();
}
Example 4 (Read operation on File ):
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fp;
int
eid;
char ename[30];
float salary;
fp
= fopen("emp.txt", "r");/*
open for writing */
fscanf(fp,
"%d %s %f", &eid, &ename, &salary);
printf("Enter the id is = %d\n", eid);
printf("Enter the name is = %s\n",ename);
printf("Enter the salary = %f \n",salary);
fclose(fp);
getch();
}
No comments:
Post a Comment