首页 > 软件 > C语言把程序的运行结果存入一个文件

C语言把程序的运行结果存入一个文件

软件 2026-10-26

用C语言如何将结果输出到一个文本文件中保存

文件的操作步骤:

#include #include int main()

{

FILE *fp;

int i, d;

fp=fopen("data.txt","w");

if(fp==NULL)

{

printf("File cannot open! " );

exit(0);

}

for(i=0; i<10; i++)

{

scanf("%d", &d);

fprintf(fp,"%d\t", d);

}

fclose(fp);

return 0;

}

格式化输出:

#include #include int main()

{

FILE *fp;

int i, No;

float salary;

fp=fopen("data.csv","w");

if(fp==NULL)

{

printf("File cannot open! " );

exit(0);

}

//输入5名员工的工号,并保存到文件中

for(i=0; i<5; i++)

{

scanf("%d %f", &No, &salary);

fprintf(fp,"%d, %.2f\n", No, salary);

}

fclose(fp);

return 0;

}

扩展资料:

从文件中读取字符:

#include #include int main()

{

FILE *fp;

char c;

if ((fp=fopen( "data.dat" , "r" ))==NULL)

{

printf("File cannot open!");

exit(0);

}

while((c=fgetc(fp))!=EOF)

putchar(c);

fclose(fp);

return 0;

}

参考资料:

百度百科-C语言

C语言中如何将程序运行的结果保存到记事本中

首先调用文件头文件 #include 然后在程序中建立一个变量 FILE *fp; fp=fopen("要保存的记事本名字 例如 a.txt","w"); 输出时 将原来的printf("%d",a); 改为 fprintf(fp,"%d",a); 注意 记事本文件要和源文件在同一个文件夹

C语言怎样将定义的函数中程序运行的结果写到文件中

#include #include #include struct student { char name[20]; int num; float score; struct student* pnext; }; int count; struct student* creat() { struct student* phead=NULL; struct student* ptail,*p; count=0; ptail=p=(struct student*)malloc(sizeof(struct student)); printf(

c语言运行的结果怎么写入文件?我是做的24点游戏,运行结束后在屏幕上显示出了计算式,想把结果写入文件。

去查:fopen,fclose,fputc,fgetc,frewind,......,FILE* fopen参数文件地址与打开方式,你的情况打开方式是"w+",返回FILE*类型指针 fclose参数指针,关闭文件 fputc参数指针,字符 fgetc参数指针,返回字符 frewind参数指针,使文件“光标”返回开头处

如何使c程序结果自动写入文档中?

C语言的程序的话,先点击开始菜单-》运行-》输入“CMD”。 假设你的程序名字是1.exe 在打开的窗口输入1.exe>>c:\jieguo.txt 这样运行完了在C盘的根目录就可以看到一个jieguo.txt的文件,运行的结果就在里面了,慢慢看吧

标签:编程 CC++ C(编程语言) 信息技术 编程语言

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved. 京ICP备11019930号-18