File Handling Tutorials
Programming
File Handling in C
File Handling in C is used to create, open, read, write, modify, and close files. It allows programs to store data permanently in secondary storage instead of keeping it only in memory.
C provides several built-in functions such as fopen(), fclose(), fprintf(), fscanf(), fgets(), fputs(), fread(), and fwrite() for file operations.
Why Use File Handling?
Store data permanently.
Read previously saved data.
Handle large amounts of information.
Exchange data between programs.
Maintain records such as student, employee, and banking data.
File Pointer
Before performing any file operation, a file pointer is required.
plaintext
FILE *fptr;FILE is a predefined structure available in the <stdio.h> header file.
Opening a File
The fopen() function is used to open a file.
Syntax
plaintext
FILE *fopen("file_name", "mode");Example
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("data.txt", "r");
if(fptr == NULL)
{
printf("File not found");
}
return 0;
}Syntax of fopen()
plaintext
FILE *fopen(filename, mode);Parameters
filename → Name or path of the file.
mode → Specifies how the file will be opened.
File Opening Modes
Read Mode
plaintext
"r"Opens an existing file for reading.
Returns NULL if file does not exist.
Write Mode
plaintext
"w"Opens file for writing.
Creates file if it does not exist.
Deletes old content if file already exists.
Append Mode
plaintext
"a"Opens file for appending.
New data is added at the end of the file.
Read and Write Mode
plaintext
"r+"Allows reading and writing.
File must already exist.
Write and Read Mode
plaintext
"w+"Allows reading and writing.
Creates a new file if needed.
Old content is removed.
Append and Read Mode
plaintext
"a+"
Allows reading and appending.
Creates file if it does not exist.
Creating a File
A file can be created using write or append modes.
Example
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("file.txt", "w");
if(fptr == NULL)
printf("File creation failed");
else
printf("File created successfully");
fclose(fptr);
return 0;
}Output
plaintext
File created successfullyWriting to a File
The following functions can write data into a file:
fprintf()
fputs()
fputc()
fwrite()
Using fprintf()
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("file.txt", "w");
fprintf(fptr, "Welcome to C Programming");
fclose(fptr);
return 0;
}Using fputs()
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("file.txt", "w");
fputs("Hello Students", fptr);
fclose(fptr);
return 0;
}Reading from a File
The following functions can read data from a file:
fscanf()
fgets()
fgetc()
fread()
Using fgets()
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
char str[100];
fptr = fopen("file.txt", "r");
fgets(str, 100, fptr);
printf("%s", str);
fclose(fptr);
return 0;
}Using fscanf()
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
char name[50];
fptr = fopen("file.txt", "r");
fscanf(fptr, "%s", name);
printf("%s", name);
fclose(fptr);
return 0;
}Closing a File
After completing file operations, always close the file using fclose().
Syntax
plaintext
fclose(file_pointer);Example
plaintext
plaintext
fclose(fptr);Closing a file releases resources used by the file.
End Of File (EOF)
EOF stands for End Of File.
Many reading functions return EOF when the file ends.
Example
plaintext
char ch;
while((ch = fgetc(fptr)) != EOF)
{
printf("%c", ch);
}Moving File Pointer
The fseek() function is used to move the file pointer to a specific location.
Syntax
plaintext
fseek(file_pointer, offset, position);Example
plaintext
fseek(fptr, 0, SEEK_SET);Moves the pointer to the beginning of the file.
ftell()
Returns the current position of the file pointer.
Example
plaintext
long pos;
pos = ftell(fptr);
printf("%ld", pos);rewind()
Moves the file pointer back to the beginning of the file.
Example
plaintext
rewind(fptr);Binary Files in C
Binary files store data in binary format rather than text format.
Common binary modes:
plaintex
rb
wb
ab
rb+
wb+
ab+Writing to a Binary File
The fwrite() function writes binary data into a file.
Syntax
plaintext
fwrite(ptr, size, count, file_pointer);Example
plaintext
#include <stdio.h>
struct Student
{
int id;
};
int main()
{
FILE *fptr;
struct Student s = {101};
fptr = fopen("student.bin", "wb");
fwrite(&s, sizeof(s), 1, fptr);
fclose(fptr);
return 0;
}Reading from a Binary File
The fread() function reads binary data from a file.
Syntax
plaintext
fread(ptr, size, count, file_pointer);Example
plaintext
#include <stdio.h>
struct Student
{
int id;
};
int main()
{
FILE *fptr;
struct Student s;
fptr = fopen("student.bin", "rb");
fread(&s, sizeof(s), 1, fptr);
printf("%d", s.id);
fclose(fptr);
return 0;
}Output
plaintext
101Common File Functions
fopen()
Creates or opens a file.
fclose()
Closes a file.
fprintf()
Writes formatted data to a file.
fscanf()
Reads formatted data from a file.
fputs()
Writes a string to a file.
fgets()
Reads a string from a file.
fputc()
Writes a single character.
fgetc()
Reads a single character.
fwrite()
Writes binary data.
fread()
Reads binary data.
fseek()
Moves the file pointer.
ftell()
Returns current file pointer position.
rewind()
Moves file pointer to the beginning.
Summary
File handling is used to store and retrieve data permanently.fopen()opens or creates a file.fprintf(),fputs(), andfwrite()are used for writing.fscanf(),fgets(), andfread()are used for reading.fclose()closes the file.fseek(),ftell(), andrewind()help manage file positions.
Binary files usefwrite()andfread()for efficient storage and retrieval.