File : A file is a container in computer storage devices used for storing data.
Types of Files
1.Text files
2.Binary files
Text files
Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad.
They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage
space.
Binary files
Binary files are mostly the .bin files in your computer. Instead of storing data in plain text, they store it in the
binary form (0's and 1's). They can hold a higher amount of data, are not readable easily, and provides better
security than text files.
File Operations
In C, you can perform four major operations on files, either text or binary:
1.Creating a new file
2.Opening an existing file
3.Closing a file
4.Reading from and writing information to a file
Working with files
When working with files, you need to declare a pointer of type file. This declaration is needed for communication
between the file and the program.
Syntax : FILE *fptr;
Modes of opening a file
r - open for reading
w - open for writing
a - open for append
r+ - open for both reading and writing
w+ - open for both reading and writing
a+ - open for both reading and appending
Predefined Functions in C Language
1.Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2.Opening an existing file (fopen)
3.Reading from file (fscanf or fgetc)
4.Writing to a file (fprintf or fputs)
5.Moving to a specific location in a file (fseek, rewind)
6.Closing a file (fclose)
Opening a file - for creation and edit
Opening a file is performed using the fopen() function defined in the stdio.h header file.
The syntax for opening a file in standard I/O is:
ptr = fopen("fileopen","mode");
Ex:
fopen("D:\\test\\guru.txt","w");
Note :
If the file guru.txt doesn't exist in the location D:\test. The first function creates a new file named guru.txt and
opens it for writing as per the mode 'w'.
fopen("D:\\test\\guru.bin","rb");
Note:
Above function opens the existing file for reading in binary mode 'rb'. The reading mode only allows you to read
the file, you cannot write into the file.
Closing a File
The file (both text and binary) should be closed after reading/writing.
Closing a file is performed using the fclose() function.
fclose(fptr);
Here, fptr is a file pointer associated with the file to be closed.
Example 1: Write to a text file
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
FILE *fptr;
fptr = fopen("D:\\guru.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
}
Example 2: Program to Open a File, Write in it, And Close the File
# include <stdio.h>
# include <string.h>
void main( )
{
// Declare the file pointer
FILE *filePointer ;
// Get the data to be written in file
char dataToBeWritten[50]="gurunadh.pahimamguru.com";
// Open the existing file GfgTest.c using fopen()
// in write mode using "w" attribute
filePointer = fopen("gurufile.c", "w") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "gurufile.c file failed to open." ) ;
}
else
{
printf("The file is now opened.\n") ;
// Write the dataToBeWritten into the file
if ( strlen ( dataToBeWritten ) > 0 )
{
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer) ;
fputs("\n", filePointer) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully written in file gurufile.c\n");
printf("The file is now closed.") ;
}
}
Example 3: Program to Open a File, Read from it, And Close the File
# include <stdio.h>
# include <string.h>
void main( )
{
// Declare the file pointer
FILE *filePointer ;
// Declare the variable for the data to be read from file
char dataToBeRead[50];
// Open the existing file gurufile.c using fopen()
// in read mode using "r" attribute
filePointer = fopen("gurufile.c", "r") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "gurufile.c file failed to open." ) ;
}
else
{
printf("The file is now opened.\n") ;
// Read the dataToBeRead from the file
// using fgets() method
while( fgets ( dataToBeRead, 50, filePointer ) != NULL )
{
// Print the dataToBeRead
printf( "%s" , dataToBeRead ) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully read from file gurufile.c\n");
printf("The file is now closed.") ;
}
}
Types of Files
1.Text files
2.Binary files
Text files
Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad.
They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage
space.
Binary files
Binary files are mostly the .bin files in your computer. Instead of storing data in plain text, they store it in the
binary form (0's and 1's). They can hold a higher amount of data, are not readable easily, and provides better
security than text files.
File Operations
In C, you can perform four major operations on files, either text or binary:
1.Creating a new file
2.Opening an existing file
3.Closing a file
4.Reading from and writing information to a file
Working with files
When working with files, you need to declare a pointer of type file. This declaration is needed for communication
between the file and the program.
Syntax : FILE *fptr;
Modes of opening a file
r - open for reading
w - open for writing
a - open for append
r+ - open for both reading and writing
w+ - open for both reading and writing
a+ - open for both reading and appending
Predefined Functions in C Language
1.Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2.Opening an existing file (fopen)
3.Reading from file (fscanf or fgetc)
4.Writing to a file (fprintf or fputs)
5.Moving to a specific location in a file (fseek, rewind)
6.Closing a file (fclose)
Opening a file - for creation and edit
Opening a file is performed using the fopen() function defined in the stdio.h header file.
The syntax for opening a file in standard I/O is:
ptr = fopen("fileopen","mode");
Ex:
fopen("D:\\test\\guru.txt","w");
Note :
If the file guru.txt doesn't exist in the location D:\test. The first function creates a new file named guru.txt and
opens it for writing as per the mode 'w'.
fopen("D:\\test\\guru.bin","rb");
Note:
Above function opens the existing file for reading in binary mode 'rb'. The reading mode only allows you to read
the file, you cannot write into the file.
Closing a File
The file (both text and binary) should be closed after reading/writing.
Closing a file is performed using the fclose() function.
fclose(fptr);
Here, fptr is a file pointer associated with the file to be closed.
Example 1: Write to a text file
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
FILE *fptr;
fptr = fopen("D:\\guru.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
}
Example 2: Program to Open a File, Write in it, And Close the File
# include <stdio.h>
# include <string.h>
void main( )
{
// Declare the file pointer
FILE *filePointer ;
// Get the data to be written in file
char dataToBeWritten[50]="gurunadh.pahimamguru.com";
// Open the existing file GfgTest.c using fopen()
// in write mode using "w" attribute
filePointer = fopen("gurufile.c", "w") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "gurufile.c file failed to open." ) ;
}
else
{
printf("The file is now opened.\n") ;
// Write the dataToBeWritten into the file
if ( strlen ( dataToBeWritten ) > 0 )
{
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer) ;
fputs("\n", filePointer) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully written in file gurufile.c\n");
printf("The file is now closed.") ;
}
}
Example 3: Program to Open a File, Read from it, And Close the File
# include <stdio.h>
# include <string.h>
void main( )
{
// Declare the file pointer
FILE *filePointer ;
// Declare the variable for the data to be read from file
char dataToBeRead[50];
// Open the existing file gurufile.c using fopen()
// in read mode using "r" attribute
filePointer = fopen("gurufile.c", "r") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "gurufile.c file failed to open." ) ;
}
else
{
printf("The file is now opened.\n") ;
// Read the dataToBeRead from the file
// using fgets() method
while( fgets ( dataToBeRead, 50, filePointer ) != NULL )
{
// Print the dataToBeRead
printf( "%s" , dataToBeRead ) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully read from file gurufile.c\n");
printf("The file is now closed.") ;
}
}














