File Handling in c
C language, allows to work with all file operations, like create, open, read, write, append, seek, delete and closing a file using FILE pointer data type.
Advantages of File handling
- Stored data in a file does not depends on computer power, It can be stored for long time.
- A file can hold large amount of data.
- A file can be transferred easily, from one to another computer or over network.
- A file data can be used a input.
File Pointer data type in C
C language provide file pointer data type to perform operations with file.
Syntax
FILE *<pointer_variable_name>;
FILE must be in capital letters.
File related operations in c
In C, with files, following operation may perform.
- Creating a new file
- Opening a file
- Reading a file
- Writing a file
- Searching a file
- Append a file
- Closing a file
- Deleting a file
In C, For reading or writing a file different file modes are provided they are:
File Access Mode | Description |
---|---|
r | Opens a file in read only mode |
w | Opens a file in write only mode |
a | Opens a file, If content exists, it does not delete the existing content. It starts writing to the file from the end of existing content. |
rb | This mode works with binary file. Opens a binary file in read only mode. |
wb | Opens a binary file in write only mode. |
ab | Opens a binary file and start writing data from the end of existing content of a file. |
r+ | Opens a file for both read and write operations. |
w+ | Opens a file for both read and write operations. |
a+ | Opens a file for both read and append operations. |
rb+ | Opens a binary file for both read and write operations. |
wb+ | Opens a binary file for both read and write operations. |
ab+ | Opens a binary file for both read and append operations. |
Open a File in C
To work with file in c, a file must be open first. To open c, provides fopen() function. fopen() opens and also creates a file, if not exist depends on provided file access mode.
Syntax
FILE* fopen(const char *file_name, const char *access_mode);
fopen() takes two arguments
const char *file_name | File name with extension to work with |
const char *access_mode | Mode to perform operation on file, like r,w,a etc. |
Fopen return value
fopen() returns a file pointer if file opens successfully and return NULL if file not found.
Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
// Declaring a file pointer
FILE* fileptr;
// Opens a file in read only mode
fileptr = fopen("myfile.txt", "r");
}
Check file exists or not in c
#include<stdio.h>
#include<stdlib.h>
void main()
{
// Declaring a file pointer
FILE* fileptr;
// Opens a file in read only mode
fileptr = fopen("myfile.txt", "r");
// Returns NULL, if file not exists
if (fileptr == NULL) {
printf("File does not exist");
}
}
Create a File in C
In C, fopen() when used with w, w+, wb, wb+, a, a+, ab, and ab+ these file modes, creates a new file with given name, if not found.
Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
// Declaring a file pointer
FILE* fileptr;
// Opens a file in write only mode
// Creates a file, if not exists
fileptr = fopen("myfile.txt", "w");
}
Reading From a File in c
To read a file, c provides different functions.
fread() | Reads from a binary file |
fgetc() | Reads a single character from file |
fgets() | Reads a single line from file |
fscanf() | Reads from file using formatted string. |
File should be opened in r access mode.
Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
// Declaring a file pointer
FILE* fileptr;
char chr;
// Opens a file in write only mode
fileptr = fopen("myfile.txt", "r");
while ((chr = fgetc(fileptr)) != EOF) {
printf("%c", chr);
}
}
Writing a File in c
To write a file, C provides following function
fprintf() | Write formatted string to a file |
fwrite() | Writes given bytes to a binary file |
fputc() | Writes a single character to file |
fputs() | Writes a single line to file |
Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
// Declaring a file pointer
FILE* fileptr;
char chr;
// Opens a file in write only mode
fileptr = fopen("myfile.txt", "w");
fputc("z", fileptr);
}
Appending content of File in c
Appending means to write without deleting or removing existing contents of file. New content adds at the end of file previous content.
File must be opened in w access mode.
Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
// Declaring a file pointer
FILE* fileptr;
char chr;
// Opens a file in append only mode
fileptr = fopen("myfile.txt", "a");
fputc("y", fileptr);
}
Closing a File in c
It is very important to close an opened file. If not done properly, file content may gets corrupted.
Syntax
fclose(file_pointer_variable);
Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE* fileptr;
fileptr = fopen("myfile.txt", "a");
fputc("x", fileptr);
fclose(fileptr); // Close an open file
}