File input/output

Location
  1. Courses

    /

  2. Quick C course

    /

  3. File text IO

Note that the functionalities presented in that page are located in the file <stdio.h>:

#include <stdio.h>

Opening a file

Before reading/writing from/to a file, we must open it. We do that by using the function 'fopen' that returns an object of type 'FILE' representing the open file. Its first argument is the path to the file and the second one is the opening mode.

Here we open a file for reading operations only:

FILE * file = fopen("file.txt", "r");

Note that the path is relative to the executable file, or, if executed from the IDE, from the root folder of the project.

Here are the opening modes:

Opening modeDescription
"r"Read only. The file must exist.
"w"Write only. The file is created or its content is cleared.
"r+"Read and write. The file must exist.
"w+"Read and write. The file is created or its content is cleared.
"a+"Read and write. All write operations are executed at the end of the file. Creates the file if it does not exits.

Note that even though some opening modes may be used to create files, it is not possible to create folders. The folders must exist.

Testing if a file is successfully open

The function 'fopen' may fail to open a file (Most likely because it does not exist or you do not have the required rights). In that case, it returns the address 0:

1
2
3
4
5
6
FILE * file = fopen("file.txt", "r"); if(file == 0) { // Error: The file is not open. }

Closing a file

When we are finished using a file, we must close it:

fclose(file);

Writing to a file

We can write to a file using the function 'fprintf'. It works like 'printf', but it takes the file to use as first argument.

We write the value of the variable 'var', of type int, to a file:

fprintf(file, "%d", var);

A complete example:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h> int main() { // Opens a file in output mode. FILE *file = fopen("file.txt", "w"); // Writes "Some text." to it. fprintf(file, "Some text."); // Closes the file. fclose(file); return 0; }

A new file, named "file.txt", has been created inside the root folder of the project. Here is its content:

Reading from a file

Without much surprise, we can use 'fscanf' to read from a file, similarly to how we read from the console using 'scanf'.

Here, we read a floating-point number, from a file, to the variable 'var':

fscanf(file, "%f", &var);

We can check if we have reached the end of a file by using the function 'feof' (It returns 1 (true) if so).

feof(file);

A complete example that reads, word by word, the content of the file that we created earlier:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h> int main() { // Opens the file "file.txt" in input mode. FILE *file = fopen("file.txt", "r"); // We will store the words read into that string. char str[50]; // As long as the end of the file has not been reached. while(feof(file) == 0) { // We read a word, from the file, into 'str'. fscanf(file, "%s", str) // We print, to the console, the word (Followed by a new line). printf("%s\n", str); } // Closes the file. fclose(file); return 0; }

Here is what is printed inside the console:

Reading a line

Using the function 'fgets', we can read a line from a file. Its first argument is the array to write the line to, the second one is the size of the array and the last one is the file to read from.

Here we read the line inside the file we created earlier:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h> int main() { // Opens the file "file.txt" in input mode. FILE *file = fopen("file.txt", "r"); // We will store each line into that array. char str[100]; // As long as the end of the file has not been reached. while(feof(file) == 0) { // We read a line from the file. fgets(str, 100, file); // We print the line. printf(str); } // Closes the file. fclose(file); return 0; }

Going back to the beginning of the file

As we read/write from/to a file, its internal cursor advances. We can set it back to the beginning by using the function 'rewind':

rewind(file);