C file handling: A file represents a sequence of bytes i.e. a text (or) a binary file for the data storage. File handling in C provides create, insert, delete, open and other operations using files. And these files going to store the data on secondary storage device i.e. hard disk. C can handle files such as stream-oriented data files(text0 and system oriented data files (binary).
File Handling in C with Examples
Stream-oriented data files
The data is stored in the same manner as it appears on the screen. And the i/o operations like data conversations, buffering, etc., take place automatically.
System-Oriented data files
These files are more closely associated with OS and data stored in memory without converting into text format.
Handling file in C
For handling the files in C, we should follow these steps:
- Naming a file
- opening a file
- Reading data from a file
- writing data into a file
- closing a file
File handling functions in C
Function | Operation |
---|---|
fopen( ) | To open a file |
fclose( ) | For closing a file |
getc( ) | Closes a file |
putc( ) | Writes a character to a file |
getw( ) | To read an integer |
putw( ) | To write an integer |
fprintf( ) | For writing a set of data in the file |
fscanf( ) | To read a set of data from the file |
fgets( ) | Read the string of characters from a file |
fputs( ) | Write a string of characters to file |
feof( ) | Detects end-of-file marker in a file |
Mode of Operations Performed on a File in C
There are many modes of opening a file. Based on the mode of a file, it can be opened for reading, writing or appending the texts. They are as follows:
- r: Opens a file in reading mode and sets the pointer to the first character in the file. It returns null if the file doesn’t exist.
- w: It opens a file in the write mode and returns null if the file could not be opened. If the file exists, data should be overwritten.
- a: Opens the file in append mode and it also returns null if a file couldn’t open.
- r+: Opens a file for reading and write mode and sets a pointer to the first character in the file.
- w+: It opens a file for reading and write mode and sets a pointer to the first character in a file.
- a+: It will open a file for reading and write mode and sets a pointer to the first character in the file. But it cannot modify existing content.
Reading from a file
The file read operations can be performed using functions fscanf or fgets. Both the functions perform the same operations as that of printf and get but with an additional parameter, the file pointer. So, it depends on you if you want to read the file line by line or character by character.
Example
FILE * filePointer; filePointer = fopen(“fileName.txt”, “r”); fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);
Writing a file
The file writes operations can be performed by the functions of filePointerrintf and filePointeruts with similarities to read operations.
Example
FILE *filePointer ; filePointer = fopen(“fileName.txt”, “w”); filePointerrintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);
Closing a file
After every successful fie operation, you must always close a file. For closing a file, you have to use a fclose function.
Example
FILE *filePointer ; filePointer= fopen(“fileName.txt”, “w”); ---------- Some file Operations ------- fclose(filePointer)
Example: / * Open, write and close a file : */
#include<stdio.h> #include<string.h> int main( ){ FILE *fp ; char data[50];// opening an existing file printf( "Opening the file test.c in write mode"); fp = fopen("test.c", "w"); if(fp == NULL ) { printf("Could not open file test.c" ); return 1; } printf("\n Enter some text from keyboard” \“ to write in the file test.c");//getting input from user while(strlen(gets(data)) > 0) { // writing in the file fputs(data, fp); fputs("\n", fp) ;}// closing the file printf("Closing the file test.c"); fclose(fp); return 0;}
Output
Opening the file test.c in write mode
Enter some text from keyboard” “ to write in the file test.c
Closing the file test.c
Example 2: /* Open, Read and close a file by using reading string by string */
#include <stdio.h> int main( ) { FILE *fp ; char data[50] ; printf( "Opening the file test.c in read mode" ); fp = fopen( "test.c", "r" ) ; if(fp == NULL ) { printf( "Could not open file test.c" ); return 1; } printf( "Reading the file test.c" ); while(fgets( data, 50, fp )!= NULL ) printf( "%s" , data ); printf("Closing the file test.c"); fclose(fp); return 0; }