代码如下:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp;
char str[90], *asd, ch;
int a1;
fp = fopen("D:\\a5.txt", "w");
if (fp == NULL)
{
printf("open mistake!\n");
exit(0);
}
printf("please input string1: ");
ch = getchar();
while( ch != '\n')
{
fputc(ch, fp);
ch = getchar();
}
fclose(fp);
printf("please input string2: ");
gets(str);
fp = fopen("F:\\a5.txt", "a");
if (fp == NULL)
{
printf("open mistake!\n");
exit(0);
}
//我认为上面没有问题
//******************************************
//问题是此处写入文件失败,原因不明
a1 = fputs(str, fp);
//******************************************
fclose(fp);
if (a1 == EOF)
{
printf("make mistake!\n");
exit(0);
}
fp = fopen("D:\\a5.txt", "r");
if (fp == NULL)
{
exit(0);
}
asd = fgets(str, 90, fp);
printf("The string is : ");
puts(asd);
fclose(fp);
return 0;
}