|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- void main()
- {
- FILE *fp;
- char ch,filename[20];
- printf("please input the filename you want to write: ");
- scanf("%s", filename);
- if( !(fp = fopen(filename,"wt+")))
- {
- printf("can not open the file!\n");
- exit(0);
- }
- printf("please input the sentences you want to write: ");
- ch = getchar();//回车字符也放在缓冲区中。在需要连续输入回车的情况下,刚输入完一个字符串,后面还需要输入另一个,需要加一个getchar(),用它来抵消那回车键,要不第二个字符串会有问题。
- ch = getchar(); //这里
- while( ch != EOF )
- {
- fputc(ch, fp);
- ch = getchar();
- }
- fclose(fp);
- }
复制代码
我在注释掉第二个 getchar(); 的时候,输出效果如下图
我有个疑问
为什么回车在输出字符串的上面呢?
如果回车没有用getchar吸收,输出效果不是应该是回车在输出的字符串的后面的吗??
求解答。
a98 发表于 2018-2-27 21:05
我的疑问是为什么后面有回车呀。
为什么不是前面呢。
如果只有一个getchar的话,回车应该在字符串最后 ...
好像是编译器的问题,vs中容易出现这种问题
|
|