How to Read from a File in C: A Step-by-Step Guide
Reading files in C is a fundamental task that allows you to read data from a file into a buffer, which can then be processed or analyzed. In this article, we will explore the different ways to read from a file in C, including using files, fopen(), and the file I/O standard library functions.
Importing the Standard Library
Before we begin, we need to import the standard library functions that we will use to read from a file. In C, we can use the following functions:
fread(): reads from a file into a bufferfscanf(): formats input into a filefreopen(): opens a file and returns a file pointer
We can include the standard library header file stdio.h at the beginning of our program.
#include <stdio.h>
Opening the File
To read from a file, we need to open the file in read mode. The file I/O standard library functions provide the following options:
freopen(): opens a file and returns a file pointerfopen(): opens a file and returns a file pointerfopen_s(): opens a file and returns a file pointer
We can use the following code to open a file:
#include <stdio.h>
int main() {
// Open a file in read mode
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Open a file in read-only mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Open a file in write mode (will not create a new file if it doesn't exist)
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Print the file name
printf("File name: %sn", file);
// Close the file
fclose(file);
return 0;
}
Reading from a File
Once we have opened the file, we can read from it using the fread() function. The fread() function takes three arguments:
file: the file pointer to the filebuffer: the buffer to store the read datasize: the size of the buffer in bytes
The fread() function returns the number of bytes read, or -1 if an error occurs.
#include <stdio.h>
int main() {
// Open a file in read mode
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Read from the file
char buffer[1024];
size_t bytes_read = fread(buffer, 1, 1024, file);
if (bytes_read == -1) {
printf("Error reading from filen");
fclose(file);
return 1;
}
// Print the contents of the buffer
printf("Buffer contents: %sn", buffer);
// Close the file
fclose(file);
return 0;
}
Writing to a File
To write to a file, we can use the fwrite() function. The fwrite() function takes three arguments:
file: the file pointer to the filebuffer: the buffer to store the write datasize: the number of bytes to write
The fwrite() function returns the number of bytes written, or -1 if an error occurs.
#include <stdio.h>
int main() {
// Open a file in write mode
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Write to the file
char buffer[1024];
size_t bytes_written = fwrite(buffer, 1, 1024, file);
if (bytes_written == -1) {
printf("Error writing to filen");
fclose(file);
return 1;
}
// Print the file name
printf("File name: %sn", file);
// Close the file
fclose(file);
return 0;
}
Creating a New File
If we want to create a new file, we can use the fopen_s() function. The fopen_s() function takes three arguments:
file: the file pointer to the filename: the name of the fileflag: the open flag (in this case,O_WRONLY)
The fopen_s() function returns the file pointer if successful, or -1 if an error occurs.
#include <stdio.h>
int main() {
// Open a file in write-only mode
FILE *file = fopen_s("example.txt", "w", O_WRONLY);
if (file == -1) {
printf("Error opening filen");
return 1;
}
// Print the file name
printf("File name: %sn", file);
// Close the file
fclose(file);
return 0;
}
Checking File Permissions
We can use the stat() function to check the file permissions of a file. The stat() function takes two arguments:
path: the path to the filemode: the file mode (in this case,S_IRUSR | S_IWUSR)
The stat() function returns a stat structure, which contains the file permissions.
#include <stdio.h>
int main() {
// Open a file in read mode
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Check the file permissions
struct stat statbuf;
if (fstat(file, &statbuf) == 0) {
printf("File permissions: %d %d %d %dn", statbuf.st_mode & S_IRUSR, statbuf.st_mode & S_IWUSR, statbuf.st_mode & S_IXUSR, statbuf.st_mode & S_IDRW);
} else {
printf("Error checking file permissionsn");
}
// Close the file
fclose(file);
return 0;
}
Using the Standard Library Functions
We can use the standard library functions to read from and write to files. The following code demonstrates how to use the fopen() function to open a file, the fread() function to read from a file, and the fwrite() function to write to a file.
#include <stdio.h>
int main() {
// Open a file in read mode
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Read from the file
char buffer[1024];
size_t bytes_read = fread(buffer, 1, 1024, file);
if (bytes_read == -1) {
printf("Error reading from filen");
fclose(file);
return 1;
}
// Print the contents of the buffer
printf("Buffer contents: %sn", buffer);
// Close the file
fclose(file);
return 0;
}
#include <stdio.h>
int main() {
// Open a file in write mode
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening filen");
return 1;
}
// Write to the file
char buffer[1024];
size_t bytes_written = fwrite(buffer, 1, 1024, file);
if (bytes_written == -1) {
printf("Error writing to filen");
fclose(file);
return 1;
}
// Print the file name
printf("File name: %sn", file);
// Close the file
fclose(file);
return 0;
}
Conclusion
Reading from files in C is a simple process that can be performed using the standard library functions. We can open a file, read from it using the fread() function, and write to it using the fwrite() function. We can also check the file permissions using the stat() function. By following this guide, you should be able to read and write to files in C with ease.
