Detailed Explanation of File Read and Write Operations in C: fread, fwrite, fprintf
In C language, file read and write operations are important means for programs to interact with external data. Through files, we can save data to disk and read data from disk. This article will detail the three main file operation functions in C language: <span>fread</span>
, <span>fwrite</span>
, and <span>fprintf</span>
.
1. Basic Concepts of Files
Before performing file operations, it is necessary to understand the following basic concepts:
- Open File: Use the
<span>fopen()</span>
function to open a file, which returns a pointer to a FILE structure. - Close File: Use the
<span>fclose()</span>
function to close an already opened file. - Error Handling: Check the return value to determine whether the file was successfully opened or read/written.
2. Using <span>fprintf</span>
2.1 Functionality
<span>fprintf</span>
is used to output formatted data to a specified text stream (usually an opened file). Its syntax is as follows:
int fprintf(FILE *stream, const char *format, ...);
2.2 Example Code
Below is an example of using <span>fprintf</span>
to write formatted strings to a text file:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "w"); // Open a text file for writing
if (fp == NULL) {
perror("Error opening file");
return -1;
}
fprintf(fp, "Hello, World!\n");
fprintf(fp, "This is an example of using fprintf in C.\n");
fclose(fp); // Close the document
return 0;
}
2.3 Explanation
In this code, we use the <span>fopen()</span>
function to create and open a new text file named “example.txt” in write mode (if the file already exists, it will be overwritten). Then we use <span>fprintf()</span>
to write several lines of content to this document. Finally, we call <span>fclose()</span>
to close the document to ensure that the content is completely saved and resources are released.
3. Using <span>fread</span>
3.1 Functionality
<span>fread</span>
is used to read data from a binary stream, and its syntax is as follows:
size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
3.2 Example Code
The following example demonstrates how to use <span>fread()</span>
to read multiple structures from a binary document:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
char name[20];
} Student;
int main() {
FILE *fp;
Student students[4];
// Assume we already have a binary document named students.bin containing student information
fp = fopen("students.bin", "rb"); // Open a binary document for reading
if (fp == NULL) {
perror("Error opening file");
return -1;
}
fread(students, sizeof(Student), 4, fp); // Read four student objects from the binary summary
fclose(fp);
for (int i = 0; i < 4; i++) {
printf("ID: %d Name: %s\n", students[i].id, students[i].name);
}
return 0;
}
3.3 Explanation
This example first defines a simple student structure. In the main program, we open a document named “students.bin” in read binary mode and then read multiple student records into a memory array at once using a single call, followed by a loop to output each student’s information, such as ID and name. After completing all operations, remember to call the <span>fclose()</span>
method to properly close this document and release resources.
4. Using <span>fwrite</span>
4.1 Functionality
In this section, we will briefly explain the functionality of <span>fwrite</span>
and its usage.
In general, <span>fwrite</span>
is used to write binary data to a file. Its syntax is as follows:
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
It is important to ensure that the data being written is correctly formatted and that the file is opened in the appropriate mode to avoid data corruption.
Conclusion:
The above is a comprehensive overview of the file read and write operation functions in C: ‘printf’, ‘fread’, and ‘fwrite’. If you have any further questions, feel free to ask for more explanations.