fopen() / fclose() / freopen()
These are the fundamental functions for opening and closing files. They are defined in <stdio.h> and serve as the starting point for all file operations. Every file you open must be closed when you are done with it.
Syntax
// Opens a file and returns a FILE pointer. // Return value: a FILE pointer on success, NULL on failure. FILE *fopen(const char *filename, const char *mode); // Closes a file. // Return value: 0 on success, EOF on error. int fclose(FILE *stream); // Associates an existing FILE pointer with a different file (useful for redirection). // Return value: stream on success, NULL on failure. FILE *freopen(const char *filename, const char *mode, FILE *stream);
Mode Strings
| Mode | Description |
|---|---|
| "r" | Opens the file for reading only. Returns NULL if the file does not exist. |
| "w" | Opens the file for writing only. Creates a new file if it does not exist; truncates the file if it does. |
| "a" | Opens the file in append mode. Creates a new file if it does not exist; writes from the end if it does. |
| "r+" | Opens the file for both reading and writing. Returns NULL if the file does not exist. |
| "w+" | Opens the file for both reading and writing. Truncates the file if it already exists. |
| "a+" | Opens the file for both reading and appending. The write position is always at the end. |
| "rb", "wb", etc. | Opens the file in binary mode. The difference from text mode is how newline characters are handled (important on Windows). |
Sample Code
#include <stdio.h>
int main(void) {
// Create a file in "w" mode and write some text to it.
FILE *fp = fopen("sample.txt", "w");
if (fp == NULL) {
// fopen failed (e.g., invalid path or insufficient permissions).
perror("fopen");
return 1;
}
fprintf(fp, "Line 1\n");
fprintf(fp, "Line 2\n");
fprintf(fp, "Line 3\n");
fclose(fp); // Always close the file after writing.
// Open the file just created in "r" mode and read its contents.
fp = fopen("sample.txt", "r");
if (fp == NULL) {
perror("fopen");
return 1;
}
char buf[256];
while (fgets(buf, sizeof(buf), fp) != NULL) {
printf("%s", buf); // Print each line to standard output.
}
fclose(fp);
// Append a new line in "a" mode (existing content is preserved).
fp = fopen("sample.txt", "a");
if (fp == NULL) {
perror("fopen");
return 1;
}
fprintf(fp, "Line 4 (appended)\n");
fclose(fp);
printf("Append complete.\n");
return 0;
}
Notes
The FILE pointer returned by fopen() is a pointer to a structure that tracks the current read/write position and state of the file. You pass this pointer to file operation functions such as fgets(), fread(), and fseek().
Always check the return value of fopen() for NULL. NULL is returned when the file does not exist or when you do not have the required permissions. Failing to check can cause a null pointer dereference and crash your program.
Always close every opened file with fclose(). Without closing, the write buffer may not be flushed and data may not be saved correctly. In addition, the number of files a process can have open is limited by the system, so leaving files open causes resource leaks.
Use fgets() to read a file line by line, and fread() / fwrite() for reading and writing binary data.
If you find any errors or copyright issues, please contact us.