File Handling in C
File Handling in c language is used to open, read, write, search or close file. It is used for permanent storage.Advantage of File
It will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium.Functions for file handling
There are many functions in C library to open, read, write, search and close file. A list of file functions are given below:No. | Function | Description |
---|---|---|
1 | fopen() | opens new or existing file |
2 | fprintf() | write data into file |
3 | fscanf() | reads data from file |
4 | fputc() | writes a character into 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 |
Opening File: fopen()
The fopen() function is used to open a file. The syntax of fopen() function is given below:- FILE *fopen( const char * filename, const char * mode );
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 |
losing File: fclose()
The fclose() function is used to close a file. The syntax of fclose() function is given below:- int fclose( FILE *fp );
C fprintf() and fscanf()
Writing File : fprintf() function
The fprintf() function is used to write set of characters into file. It sends formatted output to a stream.Syntax:
- int fprintf(FILE *stream, const char *format [, argument, ...])
- #include <stdio.h>
- main(){
- FILE *fp;
- fp = fopen("file.txt", "w");//opening file
- fprintf(fp, "Hello file by fprintf...\n");//writing data into file
- fclose(fp);//closing file
- }
Reading File : fscanf() function
The fscanf() function is used to read set of characters from file. It reads a word from the file and returns EOF at the end of file.Syntax:
- int fscanf(FILE *stream, const char *format [, argument, ...])
- #include <stdio.h>
- main(){
- FILE *fp;
- char buff[255];//creating char array to store data of file
- fp = fopen("file.txt", "r");
- while(fscanf(fp, "%s", buff)!=EOF){
- printf("%s ", buff );
- }
- fclose(fp);
- }
Hello file by fprintf...
C File Example: Storing employee information
Let's see a file handling example to store employee information as entered by user from console. We are going to store id, name and salary of the employee.- #include <stdio.h>
- void main()
- {
- FILE *fptr;
- int id;
- char name[30];
- float salary;
- fptr = fopen("emp.txt", "w+");/* open for writing */
- if (fptr == NULL)
- {
- printf("File does not exists \n");
- return;
- }
- printf("Enter the id\n");
- scanf("%d", &id);
- fprintf(fptr, "Id= %d\n", id);
- printf("Enter the name \n");
- scanf("%s", name);
- fprintf(fptr, "Name= %s\n", name);
- printf("Enter the salary\n");
- scanf("%f", &salary);
- fprintf(fptr, "Salary= %.2f\n", salary);
- fclose(fptr);
- }
Enter the id 1 Enter the name sonoo Enter the salary 120000
emp.txt
Id= 1 Name= sonoo Salary= 120000
C fputc() and fgetc()
Writing File : fputc() function
The fputc() function is used to write a single character into file. It outputs a character to a stream.Syntax:
- int fputc(int c, FILE *stream)
- #include <stdio.h>
- main(){
- FILE *fp;
- fp = fopen("file1.txt", "w");//opening file
- fputc('a',fp);//writing single character into file
- fclose(fp);//closing file
- }
a
Reading File : fgetc() function
The fgetc() function returns a single character from the file. It gets a character from the stream. It returns EOF at the end of file.Syntax:
- int fgetc(FILE *stream)
- #include<stdio.h>
- #include<conio.h>
- void main(){
- FILE *fp;
- char c;
- clrscr();
- fp=fopen("myfile.txt","r");
- while((c=fgetc(fp))!=EOF){
- printf("%c",c);
- }
- fclose(fp);
- getch();
- }
this is simple text message
C fputs() and fgets()
The fputs() and fgets() in C programming are used to write and read string from stream. Let's see examples of writing and reading file using fgets() and fgets() functions.Writing File : fputs() function
The fputs() function writes a line of characters into file. It outputs string to a stream.Syntax:
- int fputs(const char *s, FILE *stream)
- #include<stdio.h>
- #include<conio.h>
- void main(){
- FILE *fp;
- clrscr();
- fp=fopen("myfile2.txt","w");
- fputs("hello c programming",fp);
- fclose(fp);
- getch();
- }
hello c programming
Reading File : fgets() function
The fgets() function reads a line of characters from file. It gets string from a stream.Syntax:
- char* fgets(char *s, int n, FILE *stream)
- #include<stdio.h>
- #include<conio.h>
- void main(){
- FILE *fp;
- char text[300];
- clrscr();
- fp=fopen("myfile2.txt","r");
- printf("%s",fgets(text,200,fp));
- fclose(fp);
- getch();
- }
hello c programming
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.Syntax:
- int fseek(FILE *stream, long int offset, int whence)
Example:
- #include <stdio.h>
- void main(){
- FILE *fp;
- fp = fopen("myfile.txt","w+");
- fputs("This is javatpoint", fp);
- fseek( fp, 7, SEEK_SET );
- fputs("sonoo jaiswal", fp);
- fclose(fp);
- }
This is sonoo jaiswal
C rewind() function
The rewind() function sets the file pointer at the beginning of the stream. It is useful if you have to use stream many times.Syntax:
- void rewind(FILE *stream)
File: file.txt
- this is a simple text
- #include<stdio.h>
- #include<conio.h>
- void main(){
- FILE *fp;
- char c;
- clrscr();
- fp=fopen("file.txt","r");
- while((c=fgetc(fp))!=EOF){
- printf("%c",c);
- }
- rewind(fp);//moves the file pointer at beginning of the file
- while((c=fgetc(fp))!=EOF){
- printf("%c",c);
- }
- fclose(fp);
- getch();
- }
this is a simple textthis is a simple text
C ftell() function
The ftell() function returns the current file position of the specified stream. We can use ftell() function to get the total size of a file after moving file pointer at the end of file. We can use SEEK_END constant to move the file pointer at the end of file.Syntax:
- long int ftell(FILE *stream)
File: ftell.c
- #include <stdio.h>
- #include <conio.h>
- void main (){
- FILE *fp;
- int length;
- clrscr();
- fp = fopen("file.txt", "r");
- fseek(fp, 0, SEEK_END);
- length = ftell(fp);
- fclose(fp);
- printf("Size of file: %d bytes", length);
- getch();
- }
Size of file: 21 bytes
No comments:
Post a Comment
Please write your view and suggestion....