file handling in c

file handling in c

So now till we only use input or output data on console but The input and output operations that we have perform so far were done by keyboard and screen only. now till the data was input through keyboard and output on the screen. After termination of the program all we have enter data is lost because the primary memory is volatile. If data has to use later then it becomes necessary it stored in permanently storage device. So In c there are many I/O library function by which data can be stored on a secondary device or on the disk. The stored data can be access or read whenever required.

File handling in c language enables operations to create, read, update and delete the data or files stored on disk or local file system by our c program. There are some operations can be perform on file

create new file

open an existing file

reading data from file

writing data into file

delete file

functions used for file I/O operations

SR no. function Description
1 fopen() Open the new or existing file
2 fscanf() Read file data
3 fprintf() write data into the file
4 fgetc() Read a character from a file
5 fputc() Write a chacter into file
6 fclose() closed the current file
7 fseek() Set the file pointer to given position
8 fgetw() Read an integer value from a file
9 fputw() Write an integer value into a file
10 ftell() returns current position
11 rewind() set the file pointer to the begining of the file

Open file

fopen()

you must open the file before perform any operation such as read, write or update so the fopen() used to open a file let us take the example

File *fopen(const char *filename,const char *mode);

fopen() take two arguments, the first one is the name of the file to be opened and the second is the mode that decides which operation want to perform such as(read,write,append etc) on the file and fopen() returns a pointer of type FILE and if error it returns NULL. The return value of fopen() is assign to the FILE pointer that declare previously let us take example

FILE *fptr1,*fptr2;
fptr1=fopen(“myfile.txt”,”w”);
fptr2=fopen(“myfile”,”r”);

“w”(write)
if there is no file exist then this mode create a new file for writing. and if the file already exist then the previous data is erased and the new data is written into the file

“a”(Apppend)
if the file does not exist then this mode create a new file, and if the file already exist then the new data written at the end of previous data but in this mode , existing data is not remove as in “w” mode.

“r”(read)
This “r” mode used to open the existing file for reading purpose, as file to be opened must exist and the previous data of file not remove

“w+”(write+read)

This mode is same as “w” mode but in this we can also read and modify data of a file. If the file does not exist then the new file is created and file exist already then previous data erased.

“a+”(Append+read)
This mode is same as the “a” mode but in this mode we can also read the data stored in the file, if there is not exist file then new file is created if the file already exist then the new data is added at the end of previous data but in this mode we can not modify data.

“r+”(read+write)
This mode same as the “r” mode but in this mode we can also write and modify data in the file.If the file to be opened there should be exist and previous data not erased of the file. so we can add new data and update previous data.

Closing a File

The file that was open using the fopen() function must be closed when not in use or no more operations are perform on it.

Declaration: int fclose(FILE *fptr);

We we want to close Multiple files by calling single function
fcloseall();

Structure of a File program

int main()
{
FILE *fp;
fp=fopen(“filename”,”mode”);
……………
……………
……………
fclose(fp);
return 0;
}

Take the example which open a file in a write mode

#include< stdio.h >
int main()
{
FILE *fptr;
char ch;
fptr=fopen(“myfile.c”,”r”);
while(1)
{
ch=fgetc(fptr);
if(ch==EOF)
{
break;
}
printf(“%c”,ch);
}
fclose(fptr);
return 0;
}

output: file data will be print

#include< stdio.h >
int main()
{
FILE *fptr;
char ch;
fptr=fopen(“myfile.c”,”r”);
while(1)
{
ch=fgetc(fptr);
if(ch==EOF)
{
break;
}
printf(“%c”,ch);
}
fclose(fptr);
return 0;
}

fprintf() and fscanf() in c

fprintf() and fscanf() click to more

fputc() and fgetc() in c

fputc() and fgetc() click to more

fputs() and fgets() in c

fputs() and fgets() click to more