Reading and writing files in C are fundamental operations that allow you to manipulate data stored in files on disk. The C Standard Library provides several functions for these operations, mainly through the <stdio.h>
header. Below is an overview of how to read from and write to files in C.
Reading from Files
You can read data from files using various functions provided by the C Standard Library:
1. fgetc()
This function reads a single character from the file.
Syntax:
int fgetc(FILE *stream);
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r"); // Open the file in read mode
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int ch;
while ((ch = fgetc(file)) != EOF) { // Read characters until end of file
putchar(ch); // Print each character to standard output
}
fclose(file); // Close the file
return 0;
}
2. fgets()
This function reads a line from the file and stores it in a string.
Syntax:
char *fgets(char *str, int n, FILE *stream);
str
: The string where the read line will be stored.n
: The maximum number of characters to read.stream
: The file pointer.
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
char line[100];
while (fgets(line, sizeof(line), file)) { // Read a line from the file
printf("%s", line); // Print the line
}
fclose(file);
return 0;
}
3. fscanf()
This function reads formatted input from the file, similar to scanf()
.
Syntax:
int fscanf(FILE *stream, const char *format, ...);
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("data.txt", "r"); // Open the file in read mode
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int age;
char name[50];
while (fscanf(file, "%s %d", name, &age) == 2) { // Read formatted data
printf("Name: %s, Age: %d\n", name, age);
}
fclose(file);
return 0;
}
Writing to Files
You can write data to files using various functions as well:
1. fputc()
This function writes a single character to the file.
Syntax:
int fputc(int char, FILE *stream);
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "w"); // Open the file in write mode
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
for (char ch = 'A'; ch <= 'Z'; ch++) {
fputc(ch, file); // Write characters to the file
}
fclose(file);
return 0;
}
2. fputs()
This function writes a string to the file.
Syntax:
int fputs(const char *str, FILE *stream);
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
fputs("Hello, World!\n", file); // Write a string to the file
fclose(file);
return 0;
}
3. fprintf()
This function writes formatted output to the file, similar to printf()
.
Syntax:
int fprintf(FILE *stream, const char *format, ...);
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int age = 30;
fprintf(file, "Name: Alice, Age: %d\n", age); // Write formatted data
fclose(file);
return 0;
}
Important Notes
Error Checking: Always check if the file opened successfully by verifying that the file pointer is not
NULL
.File Modes: When writing to a file, be aware of the file modes (
"w"
,"a"
, etc.) to avoid losing existing data unintentionally.Buffering: C uses buffering for file I/O, so data may not be written immediately. Use
fflush(FILE *stream)
to flush the output buffer if necessary.
Summary
- Reading Files: Use functions like
fgetc()
,fgets()
, andfscanf()
to read data from files. - Writing Files: Use
fputc()
,fputs()
, andfprintf()
to write data to files. - Error Handling: Always check for errors when opening files and handle them appropriately.
Understanding file I/O in C enables you to manage persistent data effectively, allowing your programs to store and retrieve information efficiently.