File Operations, Open, Write, and Close Files In C – POFTUT

File Operations, Open, Write, and Close Files In C


While learning C programming language one of the most exciting parts is writing and reading a file. Because these operations create something on the operating system we can see which is different from other examples. In this tutorial, we will look at different aspects of file operations.

stdio.h Library

As we know C provides different types of features with libraries. Input and output related features are provided by the library named stdio.h. In order to run related file operations, we should include this library as below. We generally put include like to the start of the code file like below.

#include stdio.h

Opening File

The first step to work with a file is opening it. Files can be opened by using fopen function. fopen function generally gets the filename and mode parameters.

fopen ("test.txt", "w+");

fopen  function returns a handler back where we use FILE type variable for this.

FILE * fp;

Below we will create a file pointer named fp and open file named test.txt with w+ write and read mode.

#include <stdio.h> 
 
int main() 
{ 
   FILE * fp; 
 
   fp = fopen ("test.txt", "w+"); 
    
   return(0); 
}

Closing File

In the previous part, we have opened a file with fopen function. But the code provided there is not an efficient code because the file handler does not closed which means the file is not closed. Not closing a file can create a performance or write problems. So after our operation is completed we should close the file with fclose function.

fclose(fp);

and complete code will be like below.

#include <stdio.h> 
 
int main() 
{ 
   FILE * fp; 
 
   fp = fopen ("test.txt", "w+"); 
 
   fclose(fp); 
 
   return(0); 
}

Reading File

One of the fundamental steps for file operation is reading a file. There are different ways and modes to read a file but in this step, we simply read a line. We will put this in a while loop and read it to the end of the file. Because we will read file so we will use read mode while opening the file with fopen function. We will provide the variable, str , we want to put the grabbed string and the size to read which is 80 and the last one is the file pointer fp

fgets(str,80,fp)

And here fully working code where we use while to read line by line to the end of the file. If the end of the file arrived the NULL value will be returned.

#include <stdio.h> 
 
int main() 
{ 
   FILE * fp; 
 
   char str[80]; 
 
   fp = fopen ("test.txt", "r"); 
    
   while((fgets(str,80,fp))!=NULL) 
      printf("%s",str); 
 
   fclose(fp); 
    
   return(0); 
}

Writing File

In previous steps, we have learned how to open and close files. But the ultimate goal is not opening and closing files. We generally read or write to file. There are different ways to write a file but in this tutorial, we will simply put some line to the file. We will use fputs function by providing the string and file pointer like below.

fputs("Hi this is an example",fp);

We can see the whole working example below.

#include <stdio.h> 
 
int main() 
{ 
   FILE * fp; 
 
   fp = fopen ("test.txt", "w+"); 
 
   fputs("Hi this is an example",fp); 
 
   fclose(fp); 
    
   return(0); 
}

LEARN MORE  How To Make Directory in Python?

Leave a Comment