如何将数据写入文件?
在C语言中,你可以使用标准库中的函数将数据写入文件。以下是一些常用的方法:
使用 fprintf() 写入格式化数据
fprintf() 函数类似于 printf(),但它将数据写入文件而不是输出到标准输出。
c复制代码
#include
int main() {
FILE *file;
int number = 42;
float f = 3.14;
char str[] = "Hello, world!";
file = fopen("output.txt", "w"); // 以写入方式打开文件
if (file == NULL) {
perror("Error opening file");
return 1;