我想编写一个子程序,让用户输入他们的评论。我使用 scanf("%s", X)
来让他们输入评论,但是它只能将空格前的单词存储到字符串中。
如何解决这个问题,以便将整个句子存储到字符串或文件中?
下面是我的代码:
FILE *fp;
char comment[100];
fp=fopen("comment.txt","a");
printf("You can input your comment to our system or give opinion to the musics :\n");
scanf("%s",comment);
fputs(comment,fp);
%s
不是你想要的,你需要使用否定的扫描集。 - Mikescanf(“%[^\n]”,comment)
不会将任何内容扫描到comment
中,导致comment
未初始化。 - chux - Reinstate Monica