‘壹’ 如何把c语言中输出的数据保存到一个文件夹中
1、首先,可以先查看整体代码,了解保存整体框架。
‘贰’ 如何将在c语言中生成的数据保存到文本文件中
主要通过fprintf格式化输出函数实现,主要代码如下,
//程序功能,将10 12.345000 testinfo 写入test.txt文件
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *pf=NULL;
int m=10;
float f=12.345;
char str[20]="testinfo";
pf=fopen("test.txt", "w" );//假设test.txt文件为空
if(!pf)
{
printf("打开文件失败,程序退出!");
exit(1);
}
fprintf(pf,"%d %f %s\n",m,f,str);//写入,test.txt文件内容为10 12.345000 testinfo
if(pf)//关闭文件
{
fclose( pf);
pf=NULL;
}
printf("数据已写入test.txt文件!\n");
return 0;
}
int fprintf( FILE *stream, const char *format, ... );fprintf()函数根据指定的format(格式)发送参数到由stream指定的文件。fprintf()只能和printf()一样工作,fprintf()的返回值是输出的字符数,发生错误时返回一个负值。