Home »
C programs »
C file handling programs
C program to create, write and read text in/from file
File handling in C language: Here, we will learn to create a file, write and read text in/from file using C program, example of fopen, fclose, fgetc and fputc.
By IncludeHelp Last updated : March 10, 2024
Here, we are going to learn all about file handling with examples in C programming language, following topics we are going to cover in this article:
Steps to create, write and read text in/from file
- Creating variable of file pointer
- Opening/creating a file
- Write some of the characters in a file
- Reading one by one character from a file
- Closing a file
-
And with second example
- Writing continuous text (complete paragraph until we do not press any special character defined in program).
- Reading all text until EOF (End of the file) is not found.
- And closing the file
Example 1
In this program, we are writing some of the characters (without taking input from the keyboard) and reading, printing , written characters.
#include< stdio.h >
int main()
{
FILE *fp; /* file pointer*/
char fName[20];
printf("\nEnter file name to create :");
scanf("%s",fName);
/*creating (open) a file*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}
printf("File created successfully.");
/*writting into file*/
putc('A',fp);
putc('B',fp);
putc('C',fp);
printf("\nData written successfully.");
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);
}
printf("Contents of file is :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return 0;
}
Output
Enter file name to create : ok.txt
File created successfully.
Data written successfully.
Contents of file is :
ABC
Example 2
In this program, we are writing characters (by taking input from the keyboard) to the file until new line is not pressed and reading, printing the file.
#include< stdio.h >
int main()
{
FILE *fp; /* file pointer*/
char fName[20];
char ch;
printf("\nEnter file name to create :");
scanf("%s",fName);
/*creating (open) a file*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}
printf("File created successfully.");
/*writting into file*/
printf("\nEnter text to write (press < enter > to save & quit):\n");
while( (ch=getchar())!='\n')
{
putc(ch,fp); /*write character into file*/
}
printf("\nData written successfully.");
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);
}
printf("\nContents of file is :\n");
/*read text untill, end of file is not detected*/
while( (ch=getc(fp))!=EOF )
{
printf("%c",ch); /*print character on screen*/
}
fclose(fp);
return 0;
}
Output
Enter file name to create : ok.txt
File created successfully.
Enter text to write (press < enter > to save & quit):
Hello Guys! Learn C Programming here.
Data written successfully.
Contents of file is :
Hello Guys! Learn C Programming here.
C File Handling Programs »