C File Handling
C File Handling
The difference between the file I/O and console I/O(screen and
keyboard pair) are as below:
Before performing any file I/O, the file must be opened. While
opening the file, the following are specified:
FILE *fp;
fp=fopen(“outfile1.txt”, “w”);
1. #include<stdio.h>
2. void main( )
3. {
4. FILE *fp ;
5. char ch ;
6. fp = fopen("file_handle.c","r") ;
7. while ( 1 )
8. {
9. ch = fgetc ( fp ) ;
10. if ( ch == EOF )
11. break ;
12. printf("%c",ch) ;
13. }
14. fclose (fp ) ;
15. }
Output
int i;
scanf(“%d”, &i);
fprintf(fp,”%d\n”,i);
fclose(fp);
}
C fprintf() and fscanf()
C fseek()
Syntax:
Example:
1. #include <stdio.h>
2. main(){
3. FILE *fp;
4. fp = fopen("file.txt", "w");//opening file
5. fprintf(fp, "Hello file by fprintf...\n");//writing data into f
ile
6. fclose(fp);//closing file
7. }
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:
Example:
1. #include <stdio.h>
2. main(){
3. FILE *fp;
4. char buff[255];//creating char array to store data of file
5. fp = fopen("file.txt", "r");
6. while(fscanf(fp, "%s", buff)!=EOF){
7. printf("%s ", buff );
8. }
9. fclose(fp);
10. }
Output:
Hello file by fprintf...
Output:
Enter the id
1
Enter the name
sonoo
Enter the salary
120000
Now open file from current directory. For windows operating
system, go to TC\bin directory, you will see emp.txt file. It will
have following information.
emp.txt
Id= 1
Name= sonoo
Salary= 120000
C fputc() and fgetc()
Syntax:
Example:
1. #include <stdio.h>
2. main(){
3. FILE *fp;
4. fp = fopen("file1.txt", "w");//opening file
5. fputc('a',fp);//writing single character into file
6. fclose(fp);//closing file
7. }
file1.txt
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:
Example:
1. #include<stdio.h>
2. #include<conio.h>
3. void main(){
4. FILE *fp;
5. char c;
6. clrscr();
7. fp=fopen("myfile.txt","r");
8.
9. while((c=fgetc(fp))!=EOF){
10. printf("%c",c);
11. }
12. fclose(fp);
13. getch();
14. }
myfile.txt
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:
Example:
1. #include<stdio.h>
2. #include<conio.h>
3. void main(){
4. FILE *fp;
5. clrscr();
6.
7. fp=fopen("myfile2.txt","w");
8. fputs("hello c programming",fp);
9.
10. fclose(fp);
11. getch();
12. }
myfile2.txt
hello c programming
Syntax:
1. char* fgets(char *s, int n, FILE *stream)
Example:
1. #include<stdio.h>
2. #include<conio.h>
3. void main(){
4. FILE *fp;
5. char text[300];
6. clrscr();
7.
8. fp=fopen("myfile2.txt","r");
9. printf("%s",fgets(text,200,fp));
10.
11. fclose(fp);
12. getch();
13. }
Output:
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:
Example:
1. #include <stdio.h>
2. void main(){
3. FILE *fp;
4.
5. fp = fopen("myfile.txt","w+");
6. fputs("This is C File Handling", fp);
7.
8. fseek( fp, 7, SEEK_SET );
9. fputs("sonoo jaiswal", fp);
10. fclose(fp);
11. }
myfile.txt
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:
Example:
File: file.txt
File: rewind.c
1. #include<stdio.h>
2. #include<conio.h>
3. void main(){
4. FILE *fp;
5. char c;
6. clrscr();
7. fp=fopen("file.txt","r");
8.
9. while((c=fgetc(fp))!=EOF){
10. printf("%c",c);
11. }
12.
13. rewind(fp);//moves the file pointer at beginning of t
he file
14.
15. while((c=fgetc(fp))!=EOF){
16. printf("%c",c);
17. }
18.
19. fclose(fp);
20. getch();
21. }
Output:
this is a simple text this 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:
Example:
File: ftell.c
1. #include <stdio.h>
2. #include <conio.h>
3. void main (){
4. FILE *fp;
5. int length;
6. clrscr();
7. fp = fopen("file.txt", "r");
8. fseek(fp, 0, SEEK_END);
9.
10. length = ftell(fp);
11.
12. fclose(fp);
13. printf("Size of file: %d bytes", length);
14. getch();
15. }
Output:
Size of file: 21 bytes
Explanation:
Text vs. Binary Mode: Both binary mode and text mode are
available for opening files. While text mode is used for text
files, binary mode is appropriate for non-text files. Different
platforms (such as Windows and Unix-like systems) may treat
newline characters differently when opening a file in text mode.
In binary mode, there are no such transformations and data
is read, and written just as it is.
Conclusion:
In conclusion, the C ftell() method is a helpful resource for
locating the current file location and retrieving the file size.
Programmers may easily search through files and do actions by
combining them with the fseek() function. Successful file
handling in C depends on the understanding and use of
file opening modes, error management, and many file-
related functions. Furthermore, understanding the differences
between binary and text modes guarantees precise data
handling. C programmers who are skilled in file handling can
read, write, and modify files efficiently, enabling the creation of
various programs with exact file operations.