LectureMaterial 3
LectureMaterial 3
File handing in C is the process in which we create, open, read, write, and close
operations on a file.
o Creation of the new file
o Opening an existing file
o Reading from the file
o Writing to the file
o Closing the file
File Functions:
C language provides different functions such as fopen(), fwrite(), fread(), fseek(),
fprintf(), etc. to perform input, output, and many different C file operations in our
program.
● Portability: Without losing any data, files can be transferred to another in the
computer system. The risk of flawed coding is minimized with this feature.
● Efficient: A large amount of input may be required for some programs. File
handling allows you to easily access a part of a file using few instructions which
saves a lot of time and reduces the chance of errors.
● Storage Capacity: Files allow you to store a large amount of data without having to
worry about storing everything simultaneously in a program.
Types of Files in C
1. Text Files
2. Binary Files
1. Text Files
A text file contains data in the form of ASCII characters and is generally used to store
a stream of characters.
● Each line in a text file ends with a new line character (‘\n’).
● It can be read or written by any text editor.
● They are generally stored with .txt file extension.
● Text files can also be used to store the source code.
2. Binary Files
A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII
characters. They contain data that is stored in a similar manner to how it is stored in the
main memory.
● The binary files can be created only from within a program and their contents can
only be read by a program.
● More secure as they are not easily readable.
● They are generally stored with .bin file extension.
C-File Operations
C file operations refer to the different possible operations that we can
perform on a file in C such as:
1. Creating 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 fgets()
4. Writing to a file – fprintf() or fputs()
5. Moving to a specific location in a file – fseek(), rewind()
6. Closing a file – fclose()
Mode Description
r opens a text file in read mode
File Pointer in C
A file pointer is a reference to a particular position in the opened file. It is used in file
handling to perform all file operations such as read, write, close, etc. We use the FILE macro
to declare the file pointer variable. The FILE macro is defined inside <stdio.h> header file.
return 0;
}
Output
The file is created Successfully.
Function Description
Use formatted string and variable arguments list to take input from a
fscanf()
file.
So, it depends on you if you want to read the file line by line or character by character.
Example:
FILE * fptr;
fptr = fopen(“fileName.txt”, “r”);
fscanf(fptr, "%s %s %s %d", str1, str2, str3, &year);
char c = fgetc(fptr);
Write to a File
The file write operations can be performed by the functions fprintf() and fputs() with
similarities to read operations. C programming also provides some other functions that
can be used to write data to a file such as:
Function Description
fputs() Prints the whole line in the file and a newline at the end.
Function Description
fwrite() This functions write the specified amount of bytes to the binary file.
Example:
FILE *fptr ;
fptr = fopen(“fileName.txt”, “w”);
fprintf(fptr, "%s %s %s %d", "We", "are", "in", 2012);
fputc("a", fptr);
Closing a File
The fclose() function is used to close the file. After successful file operations, you must always
close a file to remove it from the memory.
Syntax of fclose()
fclose(file_pointer);
Example:
FILE *fptr ;
fptr= fopen(“fileName.txt”, “w”);
---------- Some file Operations -------
fclose(fptr);
Example : Program to Create a File, Write in it, and Close the File
#include <stdio.h>
#include <string.h>
int main()
{
FILE* filePointer;
char dataToBeWritten[50] = "GeeksforGeeks-A Computer "
"Science Portal for Geeks";
return 0;
}
Output:
Example: Program to Open a File, Read from it, And Close the
File
int main()
{
char dataToBeRead[50];
if (filePointer == NULL) {
printf("GfgTest.txt file failed to open.");
}
else {
printf(
"Data successfully read from file GfgTest.txt\n");
printf("The file is now closed.");
}
return 0;
}
Output:
The file is now opened.
GeeksforGeeks-A Computer Science Portal for Geeks
Data successfully read from file GfgTest.txt
The file is now closed.
Random accessing of files in C language can be done with the help of the
following functions −
ftell ( )
rewind ( )
fseek ( )
ftell ( )
It returns the current position of the file ptr.
Note − ftell ( ) is used for counting the number of characters which are entered into a
file.
rewind ( )
It makes file ptr move to beginning of the file.
For example,
FILE *fp;
-----
-----
rewind (fp);
n = ftell (fp);
printf ("%d”, n);
Output
The output is as follows −
0 (always).
fseek ( )
It is to make the file pntr point to a particular location in a file.
Position
It can have three values, which are as follows −