如何给下面C语言程序中添加输出到txt中的功能,将dos界面的结果全部追加输入到固定文本中?

2025-06-20 08:04:12
推荐回答(4个)
回答1:

不是很清楚你的题干
如果要写文件的话
if (null == (fp=fopen(“SUM.txt”,"at")){
printf("Can not open the SUM.txt file");
exit(1);
}
for
for
for
for
{
if((f=num-a-b-c-d-e)>e&&a>=1&&f<=33) {
printf("[%d]: %d=%d+%d+%d+%d+%d+%d\n",++count,num,a,b,c,d,e,f);
fprintf(fp,"[%d]: %d=%d+%d+%d+%d+%d+%d\n",++count,num,a,b,c,d,e,f);
}
fclose(fp);

}

回答2:

fprintf,不如fread好用,而且fprintf最好是用二进制文件要好一些,那样才稳定,我自从第一次用fprintf,存进去好多乱码,现在都没有用过了。

回答3:

你加一句fp=fopen(“SUM.txt”,"at");
然后fprintf(fp,"[%d]: %d=%d+%d+%d+%d+%d+%d\n",++count,num,a,b,c,d,e,f); 不就好了吗

回答4:

#include
#include
#include
using namespace std;
int main()
{
freopen("sum.txt","w",stdout);
int a,b,c,d,e,f,num,count=0;
printf("Please input the digit:\n");
scanf("%d",&num);
if(num<21)
{
printf("There are no such numbers at all!\n");
system("pause");
exit(0);
}
printf("There are following possble result:\n");
for(a=1;a<=num;a++)
for(b=1+a;b<=num-a;b++)
for(c=1+b;c<=num-a-b;c++)
for(d=1+c;d<=num-a-b-c;d++)
for(e=1+d;e<=num-a-b-c-d;e++)
{
if((f=num-a-b-c-d-e)>e&&a>=1&&f<=33)
printf("[%d]: %d=%d+%d+%d+%d+%d+%d\n",++count,num,a,b,c,d,e,f);
cout<<"["<<++count<<"[: "< }
system("pause");
return 0;
}
这是c++,如果你用的是VC6.0就可以。