Loading content...
Loading content...
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.
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.
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.
The fopen() function is used to open a file.
plaintext
FILE *fopen("file_name", "mode");plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("data.txt", "r");
if(fptr == NULL)
{
printf("File not found");
}
return 0;
}plaintext
FILE *fopen(filename, mode);filename → Name or path of the file.
mode → Specifies how the file will be opened.
plaintext
"r"Opens an existing file for reading.
Returns NULL if file does not exist.
plaintext
"w"Opens file for writing.
Creates file if it does not exist.
Deletes old content if file already exists.
plaintext
"a"Opens file for appending.
New data is added at the end of the file.
plaintext
"r+"Allows reading and writing.
File must already exist.
plaintext
"w+"Allows reading and writing.
Creates a new file if needed.
Old content is removed.
plaintext
"a+"
Allows reading and appending.
Creates file if it does not exist.
A file can be created using write or append modes.
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;
}plaintext
File created successfullyThe following functions can write data into a file:
fprintf()
fputs()
fputc()
fwrite()
plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("file.txt", "w");
fprintf(fptr, "Welcome to C Programming");
fclose(fptr);
return 0;
}plaintext
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("file.txt", "w");
fputs("Hello Students", fptr);
fclose(fptr);
return 0;
}The following functions can read data from a file:
fscanf()
fgets()
fgetc()
fread()
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;
}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;
}After completing file operations, always close the file using fclose().
plaintext
fclose(file_pointer);plaintext
plaintext
fclose(fptr);Closing a file releases resources used by the file.
EOF stands for End Of File.
Many reading functions return EOF when the file ends.
plaintext
char ch;
while((ch = fgetc(fptr)) != EOF)
{
printf("%c", ch);
}The fseek() function is used to move the file pointer to a specific location.
plaintext
fseek(file_pointer, offset, position);plaintext
fseek(fptr, 0, SEEK_SET);Moves the pointer to the beginning of the file.
Returns the current position of the file pointer.
plaintext
long pos;
pos = ftell(fptr);
printf("%ld", pos);Moves the file pointer back to the beginning of the file.
plaintext
rewind(fptr);Binary files store data in binary format rather than text format.
Common binary modes:
plaintex
rb
wb
ab
rb+
wb+
ab+The fwrite() function writes binary data into a file.
plaintext
fwrite(ptr, size, count, file_pointer);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;
}The fread() function reads binary data from a file.
plaintext
fread(ptr, size, count, file_pointer);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;
}plaintext
101Creates or opens a file.
Closes a file.
Writes formatted data to a file.
Reads formatted data from a file.
Writes a string to a file.
Reads a string from a file.
Writes a single character.
Reads a single character.
Writes binary data.
Reads binary data.
Moves the file pointer.
Returns current file pointer position.
Moves file pointer to the beginning.
File handling is used to store and retrieve data permanently.
fopen() opens or creates a file.
fprintf(), fputs(), and fwrite() are used for writing.
fscanf(), fgets(), and fread() are used for reading.
fclose() closes the file.
fseek(), ftell(), and rewind() help manage file positions.
Binary files use fwrite() and fread() for efficient storage and retrieval.