C 文件处理基础知识
在这里我们将看到一些在C语言中的基本文件处理操作。以下是这些操作的列表:
- 向文件中写入内容
- 从文件中读取内容
- 在文件中追加内容
向文件中写入内容
请参考以下代码以了解如何向文件中写入内容
示例代码
#include int main() { FILE *fp; char *filename = "sample.txt"; char *content = "Hey there! You've successfully created a file with content in c programming language."; /* open for writing */ fp = fopen(filename, "w"); if( fp == NULL ) { printf("%s: failed to open.
", filename); } /* Write content to file */ fprintf(fp, "%s
", content); if( !fclose(fp) ) printf("%s: closed successfully.
", filename); return 0; }