Home » C Programming » Tutorial » File Handling in C
A file represents a sequence of bytes of the computer hard disk. Files are used to store data permanently.
In C programming, the structure pointer of file type is used to do file I/O
FILE *fileptr;
Functions used in file handling
- fopen() create a new file or open a existing file
- fclose() closes a file
- getc() reads a character from a file
- putc() writes a character to a file
- fscanf() reads a set of data from a file
- fprintf() writes a set of data to a file
- getw() reads a integer from a file
- putw() writes a integer to a file
- fseek() set the position to desire point
- ftell() gives current position in the file
- rewind() set the position to the beginning point
Different modes used in file handling
- r opens a text file in reading mode
- w opens or create a text file in writing mode.
- a opens a text file in append mode
- r+ opens a text file in both reading and writing mode
- w+ opens a text file in both reading and writing mode
- a+ opens a text file in both reading and writing mode
- rb opens a binary file in reading mode
- wb opens or create a binary file in writing mode
- ab opens a binary file in append mode
- rb+ opens a binary file in both reading and writing mode
- wb+ opens a binary file in both reading and writing mode
- ab+ opens a binary file in both reading and writing mode
Open or Create file
The fopen() function is used to open or create any file.
Syntax of fopen() function -
FILE *fileptr = NULL;
fileptr = fopen(const char *filename, const char *mode);
filename is the name of file to open and mode represent purpose (read, write, append etc..) of opening any file.fileptr is the FILE pointer which point to file.
Closing a File
fclose() function is used to close an already opened file in program.
Syntax -
int flose(FILE *fileptr);
flclose() function closed the file and returns 0 on success of EOF char if there is an error in closing the file.The EOF is a character constant used to check end of file, defined in stdio.h header file.
Example on File Handling in C using getc() & putc()
We already discussed the functions, modes used in file operations. Here is the complete working example -
#include #include int main() { FILE *fp; char ch; int count = 0; fp = fopen("cppbuzz.txt", "w"); printf("Enter 10 characters followed by enter - \n "); while(count <10) { ch = getchar(); putc(ch,fp); count++; } fclose(fp); fp = fopen("cppbuzz.txt", "r"); while( (ch = getc(fp)) != EOF) printf("%c",ch); fclose(fp); return 0; }
Example on File Handling in C using fprintf() & fscanf()
We already discussed the functions, modes used in file operations. Here is the complete working example -
#include #include struct school { char name[10]; int fees; }; int main() { struct school xavior; FILE *fptrAppend,*fptrRead; char temp_name[10]; int temp_fees; fptrAppend = fopen("CppBuzz.txt", "a"); fptrRead = fopen("CppBuzz.txt", "r"); printf("Enter School's Name :"); scanf("%s", xavior.name); printf("Enter School's Fees :"); scanf("%d", &xavior.fees); fprintf(fptrAppend,"%s %d", xavior.name, xavior.fees); fclose(fptrAppend); do { fscanf(fptrRead,"%s %d", xavior.name, &xavior.fees); printf("%s %d", xavior.name, xavior.fees); } while(!feof(fptrRead)); return 0; }
C Tutorial
- History of C Language
- The First Program in C
- Compilation & Execution
- Compile C on Dev-C++
- Compile C on GCC Linux
- Variable & Data types
- Comments
- Storage Classes
- Conidtional Statements
- Switch Cases
- Loops (for, while, do-while)
- Arrays
- Pointers
- Function Pointer
- Strings & Library func
- Formated I/O
- Structure
- Enum
- Union
- File I/O
- Memory Management
- Error Handling
- Type Casting