user-0 提问: gets输入问题

回复:
user: user-1
content: int main(){ 	char name[N][20];  	char mix[200] = "\0"; 	int n; 	printf("请输入字符串个数："); 	scanf("%d", &n); 	rewind(stdin); 	printf("请输入%d个字符串\n", n); 	for(int i = 0; i < n; ++i) { 		gets(name[i]); 	} 	 	merger(name, n, mix); 	puts(mix); 	return 0; }  void merger(char name[][20], int n, char* mix) { 	int cnt = 0; 	for(int j = 0; j < n; ++j) { 		int len = strlen(name[j]); 		printf("%d\n", len); 		for(int k = 0; k < len; ++k) 			mix[cnt++] = name[j][k]; 		mix[cnt] = '\0'; 	} 	return; }

user: user-2
content: 赵老师说得对，scanf("%d", &n); 输入之后按下的Enter没有被清除掉，所以被后面的gets给接收了。

user: user-3
content: 运行scanf("%d",&n);这句时，你输入了“数＋回车”，但scanf只从缓冲区里面读走了数，回车符'\n'还留在输入缓冲区中。 Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

user: user-0
content: 引用 7 楼 shimachao 的回复:说错了，是留在， 不是留着。 输入过程中，回车符一般没被取走，只是被输入函数当做读入过程中的标志符。#include <stdio.h> int main() { 	char s[3][20]; 	int i; for(i = 0;i<3;++i) 	gets(s[i]);   	for(i=0;i<3;i++) 	{ 		printf("%s\n",s[i]); 	}	 	return 0; } 这一段代码，也用gets来接收字符串，总共接收三个，每个之间也是用回车分割。 如果按您说的，第一行存的应该是我们输入的第一个字符串，其中回车符没有取走，第二行的数据就应该把回车符取走，但是不是这样的？或者是第二行取走的是回车符+第二个字符串？

user: user-4
content: 说错了，是留在， 不是留着。 输入过程中，回车符一般没被取走，只是被输入函数当做读入过程中的标志符。

user: user-4
content: 引用 5 楼 ubunfans 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。您好，我在 for(int i = 0; i <= n; ++i){ gets(name[i]); }之前加了fflush(stdin)清空了输入缓冲区;问题解决了。 请问输入缓冲的数据怎么来的呢？什么时候来的？  [/quote] 就是前面输入过程中的回车符还留着缓冲区中。

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。您好，我在 for(int i = 0; i <= n; ++i){ gets(name[i]); }之前加了fflush(stdin)清空了输入缓冲区;问题解决了。 请问输入缓冲的数据怎么来的呢？什么时候来的？

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。您好，按您的说法加进去了，还是不行！我只用了一次scanf，返回值为1

user: user-3
content: 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。

user: user-5
content: 第一是0，以为你用了scanf函数后，'\n' 被留在缓冲去里面了，让后被gets函数遇到，这就被认为是第一个字符串了，所以长度死0#include<stdio.h> #include<string.h>  #define N 10 void merger(char name[N][20],int,char*); int main() {     char name[N][20];     char mix[200] = "\0";     int n;     printf("请输入字符串个数：\n");     scanf("%d",&n);     rewind(stdin); //printf("%d",n);     printf("请输入%d个字符串",n);     int i;     for(i = 0; i <= n; ++i)     {         gets(name[i]);     }      merger(name,n,mix);     puts(mix);     return 0; }  void merger(char name[10][20], int n, char* mix) {     int cnt = 0;     int j;     for(j = 0; j < n; ++j)     {         int len = strlen(name[j]);         printf("%d\n",len);         int k;     for(k = 0; k < len; ++k)     mix[cnt++] = name[j][k];     } }

user: user-6
content: 建议使用fgets, 避免缓冲区溢出。

user: user-4
content: 引用 8 楼 ubunfans 的回复:[quote=引用 7 楼 shimachao 的回复:] 说错了，是留在， 不是留着。 输入过程中，回车符一般没被取走，只是被输入函数当做读入过程中的标志符。#include <stdio.h> int main() { 	char s[3][20]; 	int i; for(i = 0;i<3;++i) 	gets(s[i]);   	for(i=0;i<3;i++) 	{ 		printf("%s\n",s[i]); 	}	 	return 0; } 这一段代码，也用gets来接收字符串，总共接收三个，每个之间也是用回车分割。 如果按您说的，第一行存的应该是我们输入的第一个字符串，其中回车符没有取走，第二行的数据就应该把回车符取走，但是不是这样的？或者是第二行取走的是回车符+第二个字符串？[/quote]  你咋没想到你前面的 printf("请输入字符串个数：\n"); 会留下回车符在缓冲区里呢 scanf("%d",&n);

user: user-0
content: 引用 1 楼 user-7 的回复:i < 1？ i <= 1输入的是两行吧哥们儿调一下我的程序试试

user: user-7
content: i < 1？ i <= 1输入的是两行吧