如何用C语言实现.txt文件中内容的添加
问题,如何用C语言中append()函数实现5、9、13行的功能:依次将1、2、3写入到D盘ccc.txt中。
void main(){ ............1
int a=4,b=2; ............2
if(a*b==8) ............3
{ ............4
//将1写入D盘ccc.txt文件中 ............5
} ............6
if(a-b==2) ............7
{ ............8
//将2写入D盘ccc.txt文件中 ............9
} ............10
if(a+a==8) ............11
{ ............12
//将3写入D盘ccc.txt文件中 ............13
} ............14
} ............15
我需要完整的main函数,请帮我把代码补充完整一下吧,谢谢~
#include "stdafx.h"
void main()
{
int a=4,b=2;
if(a*b==8)
{
//将1写入D盘ccc.txt文件中 ............5
FILE *fp;
fp=fopen("D:\\ccc.txt","a+");
if(fp==NULL)
{
printf("ccc.txt文件打开失败!");
return;
}
fprintf(fp,"%d",1);
fclose(fp);
}
if(a-b==2)
{
//将2写入D盘ccc.txt文件中 ............9
FILE *fp;
fp=fopen("D:\\ccc.txt","a+");
if(fp==NULL)
{
printf("ccc.txt文件打开失败!");
return;
}
fprintf(fp,"%d",2);
fclose(fp);
}
if(a+a==8)
{
//将3写入D盘ccc.txt文件中 ............13
FILE *fp;
fp=fopen("D:\\ccc.txt","a+");
if(fp==NULL)
{
printf("ccc.txt文件打开失败!");
return;
}
fprintf(fp,"%d",3);
fclose(fp);
}
}