// 从stdin获取字符串,输出到文件中
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *file = fopen("test.txt", "w");
if (NULL == file)
{
printf("fopen error\n");
return 0;
}
// 方法一
while (1)
{
char str[32] = {0};
if (NULL == fgets(str, 32, stdin))
{
printf("fgets error\n");
return 0;
}
else if (!strcmp(str,"#\n"))
{
printf("输入结束\n");
break;
}
if (EOF == fputs(str, stdout))
{
printf("fputs error\n");
return 0;
}
if (EOF == fputs(str, file))
{
printf("fputs error\n");
return 0;
}
}
// 方法二
/* char ch = '\0';
while ((ch = getchar()) != '\n')
{
if (EOF == putc(ch, file))
{
printf("putc error\n");
return 0;
}
} */
fclose(file);
return 1;
}