C file handling uses a FILE * stream from <stdio.h>. A program can open a file, read or write data, check errors, reposition the stream and then close it.
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
/* handle the error */
}
/* read or write */
fclose(file);
| Mode | Purpose | Important behavior |
|---|---|---|
r | Read | File must already exist. |
w | Write | Creates the file or truncates an existing file. |
a | Append | Creates the file if needed and writes at the end. |
r+ | Read and write | File must already exist. |
w+ | Read and write | Creates or truncates the file. |
a+ | Read and append | Creates the file if needed; writes are appended. |
This hub links every intentional file-handling child page in the current C section so those pages do not depend on the XML sitemap alone.
#include <stdio.h>
int main(void) {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
perror("data.txt");
return 1;
}
int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
if (ferror(file)) {
printf("An error occurred while reading the file.\n");
}
fclose(file);
return 0;
}
Key correction: use an int for the return value of fgetc() so EOF can be distinguished from valid byte values.
#include <stdio.h>
int main(void) {
FILE *file = fopen("data.txt", "r");
char line[128];
if (file == NULL) {
perror("data.txt");
return 1;
}
while (fgets(line, sizeof line, file) != NULL) {
fputs(line, stdout);
}
fclose(file);
return 0;
}
#include <stdio.h>
int main(void) {
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
perror("data.txt");
return 1;
}
fprintf(file, "Welcome to plus2net\n");
fprintf(file, "Second line\n");
fclose(file);
return 0;
}
#include <stdio.h>
int main(void) {
FILE *file = fopen("data.txt", "a");
if (file == NULL) {
perror("data.txt");
return 1;
}
fputs("Appended line\n", file);
fclose(file);
return 0;
}
The old C section used gets() for this task. gets() was removed from the C standard because it cannot limit input length. Use fgets() instead.
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *file = fopen("notes.txt", "w");
char line[100];
if (file == NULL) {
perror("notes.txt");
return 1;
}
printf("Enter lines. Press Enter on an empty line to stop.\n");
while (fgets(line, sizeof line, stdin) != NULL) {
if (strcmp(line, "\n") == 0) {
break;
}
fputs(line, file);
}
fclose(file);
return 0;
}
#include <stdio.h>
int main(void) {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
perror("data.txt");
return 1;
}
if (fseek(file, 10L, SEEK_SET) != 0) {
printf("Could not reposition the file.\n");
fclose(file);
return 1;
}
long position = ftell(file);
if (position >= 0) {
printf("Current position: %ld\n", position);
}
fclose(file);
return 0;
}
#include <stdio.h>
int main(void) {
int values[] = {1, 2, 3, 4, 5};
FILE *file = fopen("binary.dat", "wb");
if (file == NULL) {
perror("binary.dat");
return 1;
}
size_t written = fwrite(
values,
sizeof values[0],
sizeof values / sizeof values[0],
file
);
printf("Items written: %zu\n", written);
fclose(file);
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.