新手上路,请各位老师多多关照。
今晚在学习的过程中,碰到一个问题 ,代码如下:
#include
#include
int main()
{
FILE *yuan =NULL;
FILE *res2 =NULL;
yuan = fopen("111.txt","r");//打开文件
res2 = fopen("res2.txt","w");//创建res.txt
for (int i = 1;i<6;i++)
{
char date[] = "20181113";
char no[] = "010";
char bl[6];
char he[255]="";
strcpy(he,no); //no赋值
printf("%d,:%s\n",i,he);
strcat(he,date);//加date字符串
fgets(bl,255,yuan);
strcat(he,bl);
printf("%d,%s",i,he);
fputs(he,res2);
}
fclose(yuan);
fclose(res2);
return 0;
}
结果如下:
1,:010
1,01020181113aaaaa
2,:010
2,01020181113bbbbb
3,:010
3,01020181113ccccc
4,:010
4,01020181113ddddd
5,:010
5,01020181113eeeee
但是,我一开始的时候把fgets()函数的语句放在了前面,出现了问题,代码如下:
#include
#include
int main()
{
FILE *yuan =NULL;
FILE *res2 =NULL;
yuan = fopen("111.txt","r");//打开文件
res2 = fopen("res2.txt","w");//创建res.txt
for (int i = 1;i<6;i++)
{
char date[] = "20181113";
char no[] = "010";
char bl[6];
fgets(bl,255,yuan);
char he[255]="";
strcpy(he,no); //no赋值
printf("%d,:%s\n",i,he);
strcat(he,date);//加date字符串
strcat(he,bl);
printf("%d,%s",i,he);
fputs(he,res2);
//fflush(stdin);
}
fclose(yuan);
fclose(res2);
return 0;
}
结果为:
1,:
1,20181113aaaaa
2,:
2,20181113bbbbb
3,:
3,20181113ccccc
4,:
4,20181113ddddd
5,:010
5,01020181113eeeee
其中前四次的no字符串都没有了,这是什么原因呢?