File Handling in C
File is permanent
storage of data .It used to store output in files.
File handling in C
enables us to create, update, read, and delete the files stored on the local
file system through our C program. The following operations can be performed on
a file.
- Creation of the new file
- Opening an existing file
- Reading from the file
- Writing to the file
- Deleting the file
Functions for file
handling
There are many
functions in the C library to open, read, write, search and close the file. A
list of file functions are given below:
No. |
Function |
Description |
1 |
fopen() |
opens new or existing file |
2 |
fprintf() |
write data into the file |
3 |
fscanf() |
reads data from the file |
4 |
fputc() |
writes a character into the file |
5 |
fgetc() |
reads a character from file |
6 |
fclose() |
closes the file |
7 |
fseek() |
sets the file pointer to given
position |
8 |
fputw() |
writes an integer to file |
9 |
fgetw() |
reads an integer from file |
10 |
ftell() |
returns current position |
11 |
rewind() |
sets the file pointer to the
beginning of the file |
12 |
feof |
Detects end-of-file marker in a
file |
Opening File:
fopen()
We must open a file
before it can be read, write, or update. The fopen() function is used to open a
file. The syntax of the fopen() is given below.
File *fp;
The fopen()
function accepts two parameters:
We can use one of
the following modes in the fopen() function.
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 |
rb |
opens a binary file in read mode |
wb |
opens a binary file in write mode |
ab |
opens a binary file in append mode |
rb+ |
opens a binary file in read and
write mode |
wb+ |
opens a binary file in read and
write mode |
ab+ |
opens a binary file in read and
write mode |
WRITING A FILE
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
clrscr();
fp= fopen("sample.txt","w");
fputc('s',fp);
getch();
}
READING A FILE
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
FILE *fp;
clrscr();
fp= fopen("sample.txt","r");
ch=fgetc(fp);
printf("%c",ch);
fclose(fp);
getch();
}
output
s
fprintf & fscanf function
#include<stdio.h>
#include<conio.h>
main()
{
char name[40];
int age,i;
float bs;
FILE *fp;
clrscr();
fp=fopen("employee.txt","w");
if(fp==NULL)
{
printf("Cannot open file");
exit();
}
for(i=0;i<5;i++)
{
printf("\n Enter Name , age , Salary\n");
scanf("%s%d%f",&name,&age,&bs);
fprintf(fp,"%s %d %f\n",name,age,bs);
}
fclose(fp);
}
#include<stdio.h>
#include<conio.h>
main()
{
char name[40];
int age,i;
float bs;
FILE *fp;
clrscr();
fp=fopen("employee.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
}
while(fscanf(fp,"%s %d %f",&name,&age,&bs)!=EOF)
printf("%s %d %f\n",name,age,bs);
fclose(fp);
getch();
}
output
raja
20 1000.000000
somu
30 5000.000000
arun
35 6000.000000
hari
40 7000.000000
karthi
50 8000.000000
C fseek() function
The fseek() function is used to set the file
pointer to the specified offset. It is used to write data into file at desired
location.
int fseek(FILE *stream, long int offset, int whence)
Example:
#include <stdio.h>
#include<conio.h>
main(){
FILE
*fp;
fp = fopen("myfile.txt","w+");
clrscr();
fputs("welcome ", fp);
fseek( fp, 8,
SEEK_SET );
fputs("to the world", fp);
fclose(fp);
}
myfile.txt
welcome to the world
ftell()
function
To create a file name myfile1.txt in
turboc3 bin folder text file
#include<stdio.h>
#include<conio.h>
void main()
{
char fdata[50];
FILE *fstream = fopen("myfile1.txt","r");
clrscr();
printf("The
current location of th pointer before reading from the file is : %ld\n", ftell(fstream));
fscanf(fstream,"%s",fdata);
printf("The
current data read from the file is : %s\n", fdata);
printf("The
current location of th pointer after reading from the file is : %ld\n", ftell(fstream));
getch();
}
0 comments:
Post a Comment