{"id":82579,"date":"2021-07-21T09:00:35","date_gmt":"2021-07-21T03:30:35","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=82579"},"modified":"2021-07-21T09:00:35","modified_gmt":"2021-07-21T03:30:35","slug":"file-handling-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/","title":{"rendered":"File Handling in C"},"content":{"rendered":"<p>The C programming language has various features and functionalities which help in better and efficient coding. In the previous articles, we just compiled and ran the program but we didn\u2019t save the output anywhere in the system.<\/p>\n<p>But let\u2019s say that you want to store the output for future uses. Then what should you do?<\/p>\n<p>That\u2019s when the file handling feature of C comes into play. You can store the output or data onto the local file system which is volatile and it can be accessed every time.<\/p>\n<h3>What is file handling in C?<\/h3>\n<p>A file is a container in computer storage devices which is used for storing output or information permanently in the form of a sequence of bytes on the disk.<\/p>\n<p>In C, you can perform various file handling operations like creating a file, deleting a file, opening a file, reading a file or you can even manipulate the data inside of a file. You can also work with the data in your program code by fetching it from a file.<\/p>\n<h3>Need for file handling in C<\/h3>\n<p>Suppose, you want to check the output of some program several times, it will be a lazy task to compile and run the program every time. With the help of file handling, you can easily solve this problem.<\/p>\n<ul>\n<li><strong>Reusability:-<\/strong> When a program is terminated, the data will be lost. You can store your data into a file even if the program terminates, you will not lose your data.<\/li>\n<li><strong>Portability:-<\/strong> You can easily move the data of the file from one computer to another computer without any changes.<\/li>\n<li><strong>Time-Saving:-<\/strong> Let\u2019s say that you have a large number of data to enter. But it will cost you a lot of time to enter them all. You can easily overcome this problem by storing all the data into a file and later accessing them through a few commands in C.<\/li>\n<li><strong>Large storage capacity:-<\/strong> With the help of files, you can store a large number of data into a file. You don\u2019t have to worry about the storage capacity.<\/li>\n<\/ul>\n<h3>Types of Files in C<\/h3>\n<p>There are 2 types of data files present in C programming language:-<\/p>\n<h4>1. Text Files<\/h4>\n<p>You can easily read and use the text files. It is created with the .txt extension. Text files are always in human-readable format. You can read or edit the text file easily with the help of text editors like <strong>notepad<\/strong> etc. It consumes a large storage space. It does not provide any security to the data or information and stores the data or information in the form of ASCII characters.<\/p>\n<h4>2. Binary Files<\/h4>\n<p>It occupies less space and is created with the .bin extension. It stores data or information in the form of 0\u2019s and 1\u2019s. Binary files are not human-readable that\u2019s why it is more secure than text files. It is the best way to store information or data in a data file. It is much easier to access.<\/p>\n<h3>File Handling Operations in C<\/h3>\n<p>You can perform various file handling operations in C. In C, we can use file handling functions for various types of file manipulation like create, update, read or delete the files on the local file system. Below are the operations that you can perform on a file:-<\/p>\n<ul>\n<li>Creating a new file.<\/li>\n<li>Opening an existing file.<\/li>\n<li>Reading from a file.<\/li>\n<li>Writing to a file.<\/li>\n<li>Deleting a file.<\/li>\n<\/ul>\n<h3>Functions for file handling in C<\/h3>\n<p>In C, you can make use of some functions which are used for file handling purposes. Below is the list of functions:-<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Function<\/b><\/td>\n<td><b>What it does<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fopen()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a new or existing file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fprintf()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to write data into a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fscanf()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to read data from a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fputc()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to write a character into a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fgetc()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for reading a character from a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fclose()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Closes a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fseek()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to set the file pointer to a given position.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fputw()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to write an integer to a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">fgetw()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for reading an integer from a file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ftell()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to return a current position.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">rewind()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to set the file pointer at the beginning of a file.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Working with files in C<\/h3>\n<p>Before working with the files, you have to declare a pointer of type file. It is needed for the communication between the program and the file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">FILE *fptr;<\/pre>\n<h4>1. Opening or Creating a file in C<\/h4>\n<p>Before making changes to a file, you will have to open it first. You can use the <strong>fopen()<\/strong> function for that. You can also create a new file using the <strong>fopen()<\/strong> function.<\/p>\n<p><strong>Syntax of fopen():-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">FILE *fopen( char *filename, char * mode );<\/pre>\n<p>fopen() function accepts two parameters:-<\/p>\n<ul>\n<li><strong>filename:-<\/strong> Name of the file which you want to open or create. If it is stored at some specific location then you must provide the full path of the file.<\/li>\n<li><strong>mode:-<\/strong> The mode in which you want to open the file.<\/li>\n<\/ul>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fopen(\"C:\\\\temp\\\\file.txt\",\"w\");\nfopen(\"D:\\\\program.bin\",\"rb\");<\/pre>\n<p>In the above example, if the <strong>file.txt<\/strong> file does not exist in the location <strong>C:\\\\temp<\/strong> then it will create a new file called <strong>file.txt<\/strong> in that location. The mode <strong>\u2018w\u2019<\/strong> lets you create or edit the contents of a file. The second one opens an existing file for reading in binary mode in the location <strong>D:\\\\.<\/strong> The <strong>\u2018rb\u2019<\/strong> mode only allows you to read the file in binary mode. You cannot write into that file.<\/p>\n<p>Below are some modes that you can use in fopen() function:-<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Mode<\/b><\/td>\n<td><b>What it does<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">r<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to open a text file in read mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">w<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to open a text file in write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to open a text file in append mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">r+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to open a text file in read and write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">w+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to open a text file in read and write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">a+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to open a text file in read and write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">rb<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a binary file in read mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">wb<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a binary file in write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ab<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a binary file in append mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">rb+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a binary file in read and write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">wb+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a binary file in read and write mode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ab+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for opening a binary file in read and write mode<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Working of fopen() function in C<\/h4>\n<ul>\n<li>It first searches for the file at the given location.<\/li>\n<li>Then, it will load the file and place it into a buffer.<\/li>\n<li>Then, it sets up a character pointer and it points to the first character of that file.<\/li>\n<\/ul>\n<p><strong>Example of fopen() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;  \nint main( )  \n{  \nFILE *file1;  \nchar s;  \nfile1 = fopen(\"1.c\",\"w\");  \nwhile ( 1 )  \n{  \ns = fgetc(file1);  \nif(s == EOF)  \nbreak;\nprintf(\"TechVidvan Tutorial: fopen() function!\\n\");\nprintf(\"%c\",s) ;  \n}  \nfclose(file1) ;  \n}<\/pre>\n<p>You get the content of the file as output.<\/p>\n<p>In the above example, we opened a file in write mode.<\/p>\n<h4>2. Closing a file in C<\/h4>\n<p>In C, after the reading and modification of a file is done then you can close it with the fclose() function.<br \/>\n<strong>Syntax of fclose():-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fclose(filepointername);<\/pre>\n<ul>\n<li><strong>filepointername<\/strong> is a file pointer which is associated with the file.<\/li>\n<\/ul>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">FILE *file_name ; \nfile_name= fopen(\"file1.txt\", \"w\");\n\/\/ perform some file handling operations!\nfclose(file_name);<\/pre>\n<h4>3. Reading and writing to a text file in C<\/h4>\n<p>First we have to know that there are 3 types of streams in a file such as:-<\/p>\n<ul>\n<li>Input<\/li>\n<li>Output<\/li>\n<li>Input\/Output<\/li>\n<\/ul>\n<p><strong>a. Reading to a text file:-<\/strong><br \/>\nYou can use the fscanf() function to read from a file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">FILE *filepoint; \nfilepoint = fopen(\"1.txt\", \"r\");\nfscanf(filepoint, \"%d %s\", num, str2);<\/pre>\n<p><strong>b. Writing to a text file:-<\/strong><br \/>\nYou can use the fprintf() function to write to a file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">FILE *ptr ; \nptr = fopen(\"2.txt\", \"w\");\nfprintf(ptr, \"%s %s\", \"TechVidvan\", \"Tutorials!\");<\/pre>\n<p>For purposes like reading and writing to a file, input\/output operations will help you.<\/p>\n<p>You can use functions such as <strong>fprintf(), fscanf()<\/strong> for reading and writing to a text file. The one and only difference between <strong>fprintf(), fscanf()<\/strong> and <strong>printf(), scanf()<\/strong> is that the <strong>fprintf(), fscanf()<\/strong> function expects a pointer to the structure FILE.<\/p>\n<p><strong>Example of Write to a text file using fprintf()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main()\n{\nint number;\nFILE *filename;\nfilename = fopen(\"C:\\\\write.txt\",\"w\");\nif(filename == NULL)\n{\nprintf(\"Error!\");   \nexit(1);        \t \n}\nnumber = 10;\nprintf(\"TechVidvan Tutorial: Writing to a text file!\\n\");\nfprintf(filename,\"%d\",number);\nfclose(filename);\nreturn 0;\n}<\/pre>\n<p>If you compile and run the above example then it will create a file named <strong>write.txt<\/strong> in the C drive of your computer. And if you open the file, you can see that the number 10 is stored in the <strong>write.txt<\/strong> file.<\/p>\n<p><strong>Example to Read from a text file using fscanf()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main()\n{\nint number;\nFILE *filename;\nfilename = fopen(\"C:\\\\write.txt\",\"r\");\nif(filename == NULL)\n{\nprintf(\"Error!\");   \nexit(1);        \t \n}\nfscanf(filename,\"%d\",&amp;number);\nprintf(\"TechVidvan Tutorial: Reading from a file!\\n\");\nprintf(\"Value stored in 'write.txt' is: %d\",number);\nfclose(filename);\nreturn 0;\n}<\/pre>\n<p>If you run and compile the above example then it will read the value stored in write.txt file and print the value which is 10 in the computer screen.<\/p>\n<p>You can also use functions like <strong>fgetc()<\/strong> and <strong>fputc()<\/strong> in a similar way.<\/p>\n<h4>Reading and writing to a binary file in C<\/h4>\n<p>You can also read and write to a binary file in C. In case of binary files, you can make use of functions like fread() and fwrite() for reading from and writing to a file on the disk.<\/p>\n<h4>a. Writing to a binary file:-<\/h4>\n<p>You have to use the fwrite() function to write to a binary file. The fwrite() function takes 4 arguments such as:-<\/p>\n<ul>\n<li>Address of data.<\/li>\n<li>Size of data.<\/li>\n<li>Number of such types of data.<\/li>\n<li>Pointer to the file.<\/li>\n<\/ul>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fwrite(address_of_Data, size_of_Data, data_number, file_pointer);<\/pre>\n<p><strong>Example of Writing to a binary file using fwrite()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\nstruct put_two\n{\n   int num1, num2;\n};\nint main()\n{\nint i;\nstruct put_two num;\nFILE *filename;\nfilename = fopen(\"C:\\\\write_to_binary.bin\",\"wb\");\nif(filename == NULL)\n{\nprintf(\"Error!\");   \nexit(1);        \t \n}\nfor(i = 1; i &lt; 3; ++i)\n   {\n  \tnum.num1 = i;\n  \tnum.num2 = 3*i;\n  \tfwrite(&amp;num, sizeof(struct put_two), 1, filename);\n   }\nprintf(\"TechVidvan Tutorial: Writing to a binary file!\\n\");  \nfclose(filename);\nreturn 0;\n}\n<\/pre>\n<p>In the above example, we have created a file called <strong>write_to_binary.bin<\/strong> in the C drive. We also declared a structure named <strong>put_two<\/strong> with two integers such as <strong>num1<\/strong> and <strong>num2.<\/strong> We also defined it in the main function as num. After that inside the for loop, we stored the value into the file using the fwrite() function.<\/p>\n<p>The first parameter of the fwrite() function takes the address of num and the second parameter takes the size of structure <strong>put_two.<\/strong> The third parameter is set to 1 because we are only inserting one instance. And the fourth parameter points to the file in which we are storing. Atlast, we close the file.<\/p>\n<h4>b. Reading from a binary file:-<\/h4>\n<p>You can use the <strong>fread()<\/strong> function to read data from a binary file in C. The fread() function also accepts 4 arguments like the fwrite() function.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fread(address_of_Data, size_of_Data, data_number, file_pointer);<\/pre>\n<p><strong>Example of Reading from a binary file using fread()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct put_two\n{\n   int num1, num2;\n};\n\nint main()\n{\n   int i;\n   struct put_two num;\n   FILE *filename;\nfilename = fopen(\"C:\\\\write_to_binary.bin\",\"wb\");\nif(filename == NULL)\n{\nprintf(\"Error!\");   \nexit(1);        \t \n}\nfor(i = 1; i &lt; 3; ++i)\n   {\n  \tfread(&amp;num, sizeof(struct put_two), 1, filename);\n  \tprintf(\"Values are: %d\\t %d\\t\", num.num1, num.num2);\n   }\n   fclose(filename);\n   return 0;\n}<\/pre>\n<p>In the above example, you are reading from the same file called <strong>write_to_binary.bin.<\/strong> You will get the same results which you inserted in Example3 as the output.<\/p>\n<h4>Getting data using fseek() function:-<\/h4>\n<p>If you want to get the required data at a specific location then you can use the fseek() function in C. From the name. you can say that it seeks the cursor to the given record in the file.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fseek(FILE *file_pointer, long int offset, int whence);<\/pre>\n<ul>\n<li>The first parameter is the pointer to the file.<\/li>\n<li>The second parameter is the position of what you need to find.<\/li>\n<li>The third parameter determines the location from where the offset begins.<\/li>\n<\/ul>\n<p><strong>Different types of whence in fseek():-<\/strong><\/p>\n<ul>\n<li><strong>SEEK_SET:-<\/strong> Used to start the offset from the beginning of the file.<\/li>\n<li><strong>SEEK_END:-<\/strong> Used to start the offset from the end of the file.<\/li>\n<li><strong>SEEK_CUR:-<\/strong> Used to start the offset from the current location of the cursor in a file.<\/li>\n<\/ul>\n<p><strong>Example of fseek() function in C<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;  \nvoid main(){  \nFILE *filepoint;  \nfilepoint = fopen(\"2.txt\",\"w+\");  \nfputs(\"TechVidvan TutorialHi\", filepoint);  \nfseek(filepoint,18 , SEEK_SET);  \nfputs(\": Using fseek() in C!\", filepoint);  \nfclose(filepoint);  \n}<\/pre>\n<p><strong>2.txt<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">TechVidvan Tutorial: Using fseek() in C!<\/pre>\n<h3>C fputc() and fgetc():-<\/h3>\n<h4>Writing to a file:- fputc()<\/h4>\n<p>With the help of fputc() function, you can write a single character into a file.<br \/>\n<strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fputc(character, FILE *stream);<\/pre>\n<p><strong>Example of fputc() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;  \nvoid main(){  \nFILE *filepoint;  \nfilepoint = fopen(\"3.txt\", \"w\");\nfputc('T',filepoint);\nfclose(filepoint);\n}<\/pre>\n<p>The above example will insert a single character <strong>\u2018T\u2019<\/strong> in the <strong>3.txt<\/strong> file.<br \/>\n<strong>3.txt<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">T<\/pre>\n<h4>Reading from a file:- fgetc()<\/h4>\n<p>With the help of the fgetc() function, you can return a single character from a file.<br \/>\n<strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fgetc(FILE *stream);<\/pre>\n<p><strong>Example of fgetc() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nint main(){\nFILE *ptr;\nchar read;\nif (ptr = fopen(\"TechVidvan.txt\", \"r\")){\nwhile((read=fgetc(ptr))!=EOF)\nprintf(\"Content of the file is: %c\",read);\n}\nfclose(ptr);\nreturn 0;\n}<\/pre>\n<p>The above example will read from the file called <strong>TechVidvan.txt<\/strong> and display the contents of that file on the screen.<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: A great place to start learning the C programming language!<\/div>\n<h4>fgets() and fputs() function in C:-<\/h4>\n<p>You can use fgets() and fputs() functions to write and read strings from a file.<\/p>\n<h4>Writing to a file:- fputs() function<\/h4>\n<p>WIth the help of fputs() function, you can write a line of characters into a file.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fputs(char *string, FILE *filepointer);<\/pre>\n<p><strong>Example of fputs() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;  \n#include&lt;conio.h&gt;  \nvoid main(){  \nFILE *file;  \nfile=fopen(\"puts.txt\",\"w\");  \nfputs(\"TechVidvan Tutorial: Writing to a file in C!\",file);  \nfclose(file);  \ngetch();  \n}<\/pre>\n<p>After compiling and running the above program, it will write the line \u201cTechVidvan Tutorial: Writing to a file in C\u201d in a file called <strong>puts.txt.<\/strong><\/p>\n<p><strong>puts.txt<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">TechVidvan Tutorial: Writing to a file in C!<\/pre>\n<h4>Reading from a file:- fgets() function<\/h4>\n<p>With the help of fgets() function, you can read a line of characters from a file.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fgets(char *string, length, FILE *ptr);<\/pre>\n<p><strong>Example of fgets() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;  \n#include&lt;conio.h&gt;  \nvoid main(){  \nFILE *file;  \nchar strings[300];  \nfile=fopen(\"puts.txt\",\"r\");  \nprintf(\"Content of the file is: %s\",fgets(strings,200,file));  \nfclose(file);  \ngetch();  \n}<\/pre>\n<p>After compiling and running the above program, it will display the data which is stored inside the <strong>puts.txt<\/strong> file. And it will display the data on the screen.<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Writing to file in C!<\/div>\n<h3>Summary<\/h3>\n<p>In this tutorial, we have discussed various file handling operations in C. We learnt that there are 2 types of data files in C. We also discussed several file handling functions in detail. With the help of file handling functions, you can create, edit, open, delete a file. You can also perform some file manipulating methods as well. Functions such as <strong>fprintf(), fscanf(), fgetc(), fputc(), fgets(), fputs()<\/strong> will help you in writing and reading to a file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C programming language has various features and functionalities which help in better and efficient coding. In the previous articles, we just compiled and ran the program but we didn\u2019t save the output anywhere&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82893,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3797,3798,3799,3800,3801,3802,3803,3804],"class_list":["post-82579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-fgetc-in-c","tag-fgets-in-c","tag-file-handling-in-c","tag-file-handling-operations-in-c","tag-fprintf-in-c","tag-fputc-in-c","tag-fputs-in-c","tag-fscanf-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>File Handling in C - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what is file handling in C with its operations. See the types of data files in C &amp; file handling functions like create, edit, open etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"File Handling in C - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what is file handling in C with its operations. See the types of data files in C &amp; file handling functions like create, edit, open etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-21T03:30:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/File-Handling-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"File Handling in C - TechVidvan","description":"Learn what is file handling in C with its operations. See the types of data files in C & file handling functions like create, edit, open etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/","og_locale":"en_US","og_type":"article","og_title":"File Handling in C - TechVidvan","og_description":"Learn what is file handling in C with its operations. See the types of data files in C & file handling functions like create, edit, open etc.","og_url":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-21T03:30:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/File-Handling-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"File Handling in C","datePublished":"2021-07-21T03:30:35+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/"},"wordCount":2155,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/File-Handling-in-C.jpg","keywords":["fgetc() in C","fgets() in C","File Handling in C","File Handling Operations in C","fprintf() in C","fputc() in C","fputs() in C","fscanf() in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/","name":"File Handling in C - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/File-Handling-in-C.jpg","datePublished":"2021-07-21T03:30:35+00:00","description":"Learn what is file handling in C with its operations. See the types of data files in C & file handling functions like create, edit, open etc.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/File-Handling-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/File-Handling-in-C.jpg","width":1200,"height":628,"caption":"File Handling in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/file-handling-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"File Handling in C"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=82579"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82579\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82893"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=82579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=82579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=82579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}