025. 读写文件内容
)
025. 读写文件内容
写入文件
以下是一个完整的示例,展示如何打开文件、写入内容并关闭文件。
示例:写入文件
#include <stdio.h>
int main() {
FILE *file;
// 打开文件
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Failed to open the file.\n");
return 1;
}
printf("File opened successfully.\n");
// 写入内容
fprintf(file, "Hello, World!\n");
printf("Content written to the file.\n");
// 关闭文件
if (