File is the collection of records, be it a text data or a binary one. C programming allows you to handle files on your storage with greater ease.
1. Open a file
2. Write data to a file.
3. Read data from a file
4. Close a file
A file can be created or opened via C programming by using the fopen() function. Parameters filename and file handling mode must be passed through the fopen() function.
Mode | Description |
---|---|
w | Opens a file in write mode . If the file exists, it will open the same file and the existing data will be lost. If the file doesn’t exist, it will create and open a new file. |
w+ | Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. |
r | Opens an existing fle in read mode. It the file doesn’t exist, it returns null pointer. |
r+ | Opens a text file for both reading and writing. |
a | Opens an existing file in write mode for adding the new records. If the file doesn’t exist, it will create and open a new file. |
a+ | Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended. |
Syntax :
File fopen("filename","mode");
You can use functions like fputc(), fprintf() or fputs() to write data on a file via C programming.
fputc() is used to write a character on a file. It returns the character written on success or End of File (EOF) on failure.
fputc( variable, FILE);
fprintf() and fputs() are used to write a string on the file. They return a non negative value on success or EOF on failure.
fprintf(FILE,string);
fputs(string,FILE);
Example :
#include <stdio.h> main () { FILE *fp; int c = 'A'; fp = fopen("new.txt", "w"); fputc(c, fp); fprintf(fp, "\nLearning file handling in C Programming.\n"); fputs("This is my first code to create and write on a file via C.\n", fp); fclose(fp); return(0); }
You can use funcitons like fgetc(), fscanf() or fgets() to read data from a file on C programming.
fgetc() is used to read a character from the referenced file. It returns the character on success or EOF on failure.
fgetc(FILE);
fscanf() is used to read a string. It stops when a white space is encountered.
fscanf(FILE, "%s", variable);
fgets() is used to read a group of strings together. It stops when it encounters a new line or the EOF.
fgets(variable, int size, FILE);
Example :
#include <stdio.h> main() { FILE *fp; char first, more[255]; fp = fopen("new.txt", "r"); first = fgetc(fp); printf("Line 1 :%c\n",first); fscanf(fp, "%s", more); printf("Line 2 : %s\n", more ); fgets(more, 255, fp); printf("Line 3: %s\n", more ); fgets(more, 255, fp); printf("Line 4: %s\n", more ); fclose(fp); }
You can use function fclose( ) to close a file.
fclose(FILE);
You can use functions namely fread() and fwrite() to write and read data in binary format. Data stored in binary format is not human readable because it is stored in encrypted form. Binary file requires less memory space in comparision to text file. At the end of a text file, EOF character is stored to mark the end of the file and its ascii value would be 26 while no such character is added to mark the end in binary file.
Syntax :
fread(variable, size, count, FILE);
fwrite(variable, size, count, FILE);
Example :
#include <stdio.h> #include <string.h> int main () { FILE *fp; char c[] = "Learning File Handling in C Programming"; char data[100]; fp = fopen("new.bin", "w+"); /* Write data to the file */ fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */ fseek(fp, 0, SEEK_SET); /* Read and display data */ fread(data, strlen(c)+1, 1, fp); printf("%s\n", data); fclose(fp); return(0); }
Leave a comment