C File I/O

Summary: in this tutorial, you will learn how to use C File I/O functions including opening and closing a file.

File Pointer

C provides you with a set of functions that deal with file I/O effectively. Since these functions come from the stdio.h, you need to include the stdio.h library in your program.

To access a file, you need a file pointer which is the memory address of the file. The following shows how to declare a file pointer:

FILE* fp;
Code language: C++ (cpp)

Opening a file

Before reading from / or writing to a file, you need to open it. To open a file, you use the fopen() function. The following illustrates the fopen() function:

FILE *fopen(const char *file_name, const char *mode);
Code language: C++ (cpp)

In the fopen()function:

  • The  *file_name specifies the path to file that you want to open.
  • The mode parameter determines if the file is open for reading, writing, or appending. See the table below for more detail.

The fopen() function returns a FILE pointer (or file handle). It returns a NULL pointer if the file does not exist or write-protected.

The following table shows the valid values for the mode when openning a file:

ModeMeaning
rOpen a file for reading.
wOpen a file for writing. If the file exists, the fopen() function deletes all the contents of the file before writing new data. Otherwise, the fopen() function creates a new file.
aOpen a file for appending. The fopen() function creates the file if it does not exist.
r+Open a file for reading and writing.
w+It’s a combination of r and w mode.
a+Open a file for appending. The fopen() function appends the contents of the file if it the file exists

These modes apply to the text files. For the binary files, you just need to add the character b at the end of each mode such as rb, wb, ab, and r+b.

Notice that the mode argument is a string, not a character. Therefore, you need to include the mode argument in double quotes (“) when passing it to the fopen() function.

The following example uses the fopen() function to open a text file named readme.txt in the same directory with the program:

FILE* fp; fp = fopen("readme.txt","r");
Code language: C++ (cpp)

Closing a file

After processing a file, you need to close it using the fclose() function. The following shows the syntax of the fclose() function:

int fclose(FILE *fp);
Code language: C++ (cpp)

The fclose() function flushes the stream pointed by the file pointer *fp and closes the file descriptor.

The  fclose() function returns EOF or error code to indicate the error if an error occurred. If everything is fine, it returns 0.

The following example shows how to use the fclose() function to close a file referenced by the pointer fp:

fclose(fp);
Code language: C++ (cpp)

Putting it all together

The following example shows how to open the file readme.txt and close it:

#include <stdio.h> int main() { char *filename = "readme.txt"; // open the file specified by the filename FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Cannot open the file %s", filename); return 1; } printf("File %s opened\n", filename); // close the file fclose(fp); }
Code language: C++ (cpp)

Summary

  • Include the stdio.h in your program to use the file I/O functions
  • Use the fopen() function to open a file.
  • Use the fclose() function to close the file once you complete working with it.
  • Always close the file before the program exits.
Was this tutorial helpful ?