Text IO with files

Text IO functions

The functions to input and output text data from/to files are very similar to the functions to read and write text data form/to the console. The main difference is that they take the file stream to input/output from/to in argument.

Writing a character to a stream

There are two functions to output a character to a stream. They do the same, only the name is different. Their header are int fputc(int character, FILE* stream) and int putc(int character, FILE* stream). The first argument is the character to output and the second one is the stream to output it to. If the operation is successful, it returns the character back. Otherwise, the value of the macro EOF is returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "w"); fputc('A', file); // Writes the letter 'A' to the stream 'file' (Linked to the file "file.txt"). putc('B', file); // Outputs the letter 'B' to the stream 'file' (Linked to the file "file.txt"). fclose(file); // Closes the stream (Linked to the file "file.txt"). return 0; }

Writing a string to a stream

The header of the function to output a string of characters to a stream is int fputs(const char* str, FILE* stream). The first argument is the null-terminated string to write and the second one is the stream to output it to. If the operation is successful, it returns a non-negative value. In case of error, the value of the macro EOF is returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "w"); fputs("I am a string.", file); /* "I am a string." is written to the stream 'file' (Linked to the file "file.txt"). */ fclose(file); // Closes the stream (Linked to the file "file.txt"). return 0; }

Reading a character from a stream

There are also two functions to input a character from a stream and they also do the same. The only difference is in the name. Their header are: int fgetc(FILE *stream) and int getc(FILE* stream). The argument is the stream to read the character from. If the operation is successful, it returns the character read. Otherwise, it returns the value of the macro EOF.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "r"); char c, c2; /* Reads a character from the stream 'file' (Linked to the file "file.txt") into the variable c. */ c = fgetc(file); /* Reads a character from the stream 'file' (Linked to the file "file.txt") into the variable c2. */ c2 = getc(file); /* Prints the 2 characters read. */ printf("%c %c\n", c, c2); fclose(file); // Closes the stream 'file' (Linked to the file "file.txt"). return 0; }

Reading a string from a stream

The header of the function to input a string from a stream is char * fgets(char* str, int num, FILE* stream). It reads characters until at least one of the following conditions is achieved: A newline character has been read, the amount of character specified by the second argument as been read or the end of the file (stream) has been reached. The first argument is a pointer to an array of char where the characters read will be stored. The second one is the size of the array given as first argument (So it represents the maximum amount of character that can be read (The null character included)). The last argument is the stream to input from. In case a newline character is read, it is also written to the array given in first argument. It always appends a null-character at the end of the string written in the array given as first argument (To make it a null-terminated string). If the string has been successfully read, a pointer to the array given as first parameter is returned. Otherwise, a null pointer is returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "r"); char array[50]; /* Reads a string from the stream 'file' (Linked to the file "file.txt") into the array 'array'. */ fgets(array, 50, file); /* Prints the string read, to the console. */ printf("%s\n", array); fclose(file); // Closes the stream 'file' (Linked to the file "file.txt"). return 0; }

Send back a character to the stream

The function ungetc, which header is int ungetc(int character, FILE* stream) gives a character to the stream that will then be read the next time a character is inputted from the stream. Note that the character is temporarily saved in the memory and not inside the actual stream (file). The first parameter is the character to send to the stream and the last one is the stream to send it to. In case of success, the character is returned back, otherwise, the value of the macro EOF is returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "r"); char c = 'z'; /* Sends the character 'z' to the stream 'file'. */ ungetc(c, file); /* Reads a character from the stream file. The character 'z' is retrieved. */ c = fgetc(file); /* Prints the character 'z' to the console. */ putchar(c); return 0; }

Write formatted text data to a stream

The header of the function to output formatted text data to a stream is int fprintf(FILE* stream, const char* format, ...). It works the same way as the function printf, except that it takes one more argument, which is the stream to output to, as the first parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "w"); /* Writes the integer 12345, followed by a newline character, to the stream 'file'. */ fprintf(file, "%d\n", 12345); double n = 500.78; /* Writes the value of the floating-point variable 'n', followed by a newline character, to the stream 'file'. */ fprintf(file, "%f\n", n); fclose(file); return 0; }

The content of the created file:

Read formatted text data from a stream

The header of the function to input formatted text data from a stream is int fscanf(FILE* stream, const char* format, ...). It works the same way as the function scanf, except that it takes one more argument, which is the stream to input from, as the first parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "r"); int a; double b; /* Reads an integer (Written in text) from the stream 'file'. */ fscanf(file, "%d", &a); /* Prints the integer read to the variable a, to the console. */ printf("%d\n", a); /* Reads a floating-point number (Written in text), as a value of type double, from the stream 'file'. */ fscanf(file, "%lf", &b); /* Prints the floating-point number read to the variable b, to the console. */ printf("%f\n", b); fclose(file); return 0; }