File Input/Output (I/O) is a fundamental concept in programming that allows you to read data from and write data to files on your computer. This topic matters because it enables you to work with external data sources, such as text files or databases, which can be crucial for a wide variety of applications like data analysis, system configuration, and web development. By the end of this lesson, you'll learn how to perform basic file I/O operations using C language.
In C, files are treated as streams of bytes. A file stream is an abstract representation of a file, which allows us to read from or write to the file using standard I/O functions.
To work with a file in C, you must first open it by creating a file stream associated with that file. This is done using the fopen()
function:
FILE *file_stream = fopen("filename", "mode");
Here, "filename" specifies the name of the file to be opened, and "mode" defines how the file should be opened. The possible modes are as follows:
"r"
: Open a file for reading (default behavior)."w"
: Open a file for writing. If the file already exists, its contents will be destroyed."a"
: Open a file for appending. If the file does not exist, it will be created."r+"
: Open a file for both reading and writing (update mode)."w+"
: Open a file for both reading and writing (truncate mode)."a+"
: Open a file for both reading and appending.To read data from a file, you can use the fgets()
function or the fscanf()
function.
char line[100];
int num;
// Using fgets
if (fgets(line, sizeof(line), file_stream) != NULL) {
printf("Read data: %s", line);
}
// Using fscanf
if (fscanf(file_stream, "%d", &num) == 1) {
printf("Read number: %d", num);
}
To write data to a file, you can use the fprintf()
function.
if (fprintf(file_stream, "Hello, World!\n") > 0) {
printf("Wrote data successfully.");
}
After you're done working with a file, it is essential to close the associated file stream using the fclose()
function.
if (fclose(file_stream) == 0) {
printf("Closed file successfully.");
}
Let's read a text file named "data.txt" line by line and print its contents:
#include <stdio.h>
int main() {
FILE *file = fopen("data.txt", "r");
char line[100];
if (file != NULL) {
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
if (fclose(file) == 0) {
printf("File closed successfully.");
}
} else {
printf("Could not open the file.");
}
return 0;
}
Let's create and write data to a new text file named "output.txt":
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "w");
if (file != NULL) {
fprintf(file, "Hello\nWorld\n!");
if (fclose(file) == 0) {
printf("File closed successfully.");
}
} else {
printf("Could not open the file.");
}
return 0;
}
What causes it: This error occurs when you haven't included the necessary header file stdio.h
.
Solution: Include the following line at the beginning of your code to resolve this issue:
#include <stdio.h>
What causes it: This error occurs when you're trying to read data into a variable of the wrong type. For example, reading an integer into a character array.
Solution: Make sure that the format specifier and the target variable match when using fscanf()
.
What causes it: This error occurs when you forget to put a semicolon (;) at the end of a control statement like if
, while
, or for
.
Solution: Add a semicolon at the end of each control statement.
What causes it: This error occurs when you try to open a non-existent file or specify an incorrect file path.
Solution: Ensure that the file exists and the provided file path is correct. If necessary, create the file before trying to open it.
What causes it: This error occurs when there's an issue with opening the file, such as insufficient permissions or an incorrect mode specified for opening the file.
Solution: Check that you have the necessary permissions to read/write the file and ensure that the provided mode for fopen()
is appropriate for your use case.
fopen()
to open files in various modes for reading or writing.fgets()
, fscanf()
, or similar functions.fprintf()
.fclose()
.