【发布时间】:2016-10-18 14:56:21
【问题描述】:
所以我用以下代码搞乱了读取函数 fgets 和 scanf 以及打印函数 write 和 printf:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
printf("Enter an integer: ");
int n = 0; scanf("%d",&n);
printf("The input number is: %d\n", n);
printf("Please enter a string now: ");
char buffer[200];
read(0,buffer,200);
printf("The input string is: %s", buffer);
printf("which is: %s\n", buffer);
printf("Now please enter another message: ");
fgets(buffer,200,stdin);
write(1,buffer,200);
return 0;
}
我会收到这些错误:
1-在第一次scanf之后,它不会只显示输入字符串的消息。
2-我现在写的就是要保存在字符串中的内容。
3-它会跳过最后的 fgets...
这根本没有任何意义;我想得到这样的输出:
Enter an integer: 15
The input number is: 15
Please enter a string now: This is the message1
The input string is: This is the message1 which is: This is the message1
Now please enter another message: This is the message2
This is the message2
感谢您的帮助!
【问题讨论】:
-
read不会将空终止符添加到您的buffer -
和
write输出整个buffer,也不是初始化项。 -
不要发布文字图片。将文本作为文本发布!