Reading from file using C
Files are used for storing data as a sequence of bytes either as text or in binary format. This article will show you how Reading from file using C works and writing code for it. Text files (.txt files) are created easily by using simple text editors which can be maintained in a easy manner whereas binary file (.bin files in your computer) stores data as 0s and 1s which cannot be easily readable ensures more security than text files. Storage space required for binary file is less.
One of the IDE which can be used for c code writing is codeblocks.
Data stored in these files can be handled by using C. Operations like reading or writing in existing file, creating new file and closing a file can be done by C. Now lets see how to reading from file using C works.
Reading from file using C
C language program read the file which was entered by user and output its information on the screen. While working with file, you need to create a pointer variable for type file. This variable is used for the file and program communication. The program file which executes a file should be in same directory in which file to be read is present.
Standard Input and output functions in C are predefined structures in header file stdio.h. Function used to open a file is fopen(), it returns a pointer to stdio.h file when file successfully opened else it returns NULL. While opening a file we are bringing data from disk or from storage devices to RAM where operations can be performed on it [like reading, editing].The prototype of fopen function is —-
Fopen ( const char * filename, const char * mode )
Filename is a string in which you use the name your file and access mode has one of the following values.
r – Opens a file for reading purpose.
w – Opens a file for writing purpose, if file doesn’t exist it creates a new one.
a – Opens a file for writing in appending mode where it start appending content in the existing file with the new created one . And r+, w+, a+ are also the mode that works with combination of above modes.
Example: fopen(sample.txt, “r”); —-> here sample.txt file is opened in reading mode .you can just read its content, can’t edit or so.
A character that is read from the file is returned by function fgetc(). Closing of file is done by fclose () function.
C program to read a file
Code in C programming to open a file and display its contents on screen.
#include <stdio.h> #include <stdlib.h> int main() { char ch, file_name[25]; FILE *fip; printf("Enter the file name to read\n"); gets(file_name); fip = fopen(file_name, "r"); // read mode if (fip == NULL) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } printf("The contents of %s file are:\n", file_name); while((ch = fgetc(fip)) != EOF) printf("%c", ch); fclose(fip); return 0; }
Pointer variable we used here is fip which return NULL when it can’t find the requested file. Perror() function print the error message. When we compile and run the above program, if it displays the following message, then it means we are trying to open a file that does not exist.
“Error while opening the file”
Otherwise the content in the file is read by fgetc() function. This function read a character and print it using printf() function where % c is the datatype of character to be printed. This process repeats till the end of file [EOF] using while loop. EOF macro is a negative value with int datatype, that is returned by functions to know end-of-file [i.e No more data from stream]. EOF is defined in header file stdio.h (and is usually -1). Once file reading is done file is closed by fclose() function. You can other c projects or c++ projects here.
Leave a Reply