简体   繁体   English

字符数组接受输入,将剩余数据放入连续的字符数组

[英]Char array taking input putting remaining data to consecutive char array

I am having a structure defined in person.h file 我在person.h文件中定义了一个结构

struct person{
    char firstName[11];
    char familyName[21];
    char telephone[11];
    int isStudent;
};

Now I want to take input of firstName,familyName etc. 现在我要输入firstName,familyName等的输入。

So I did something like this : 所以我做了这样的事情:

void AddRecord(struct person person[20],int currentElemetCount,int maxCount){
    getFirstName(person[currentElemetCount].firstName);
    getFamilyName(person[currentElemetCount].familyName);
}

In other commons.c file I define these functions, 在其他commons.c文件中,我定义了这些功能,

 void getFirstName(char firstName[10]){
    printf("Enter First Name : ");
    scanf("%10s", firstName);
 }

 void getFamilyName(char familyName[20]){
    printf("Enter Family Name : ");
    scanf("%20s", familyName);
 }

But when I execute this program, in actual case I want that if user enter more than 10 characters for first name then simply ignore them. 但是,当我执行此程序时,在实际情况下,我希望如果用户输入的名字超过10个字符,则只需忽略它们。 But here they are getting assigned to familyName and am not able to input anything for familyName. 但是在这里,它们被分配给familyName,并且不能为familyName输入任何内容。

What can be the reason ? 可能是什么原因? Please help. 请帮忙。

Say if input is "avshgvdshvdhsdhsdhsdhdh" a very long string. 假设输入的字符串“ avshgvdshvdhsdhsdhsdhdh”很长。 Then I want firstName char array as "avshgvdshv" . 然后我想要firstName char数组为“ avshgvdshv”。 But with this code remaining "dhsdhsdhsdhdh" string is getting assigned to familyName array, which I dont want. 但是,剩下的这段代码将“ dhsdhsdhsdhdh”字符串分配给不希望的familyName数组。

  1. You need to clear stdin (the input stream) 您需要清除stdin (输入流)
  2. Use fgets 使用fgets

The excess data (over 10 characters) remains in the input stream and are used the next time you call scanf . 多余的数据(超过10个字符)保留在输入流中,并在下次调用scanf

To clear stdin 清除stdin

int ch;
while ((ch = getchar()) != '\n' && ch != EOF);

A portion of code I used in my own programs to get input using fgets and clearing the input stream to allow for other input: 我在自己的程序中使用的一部分代码使用fgets获取输入,并清除输入流以允许其他输入:

fgets(c, size, stdin);
if (c[strlen(c) - 1] != '\n')
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
}

The fgets function takes input until it runs into a newline ( \\n ) or the maximum number of bytes readable have been read (in your case 10). fgets函数接受输入,直到遇到换行符( \\n )或已读取最大可读字节数(在您的情况下为10)。 Usually fgets gives a newline terminated string ( char* with \\n at the end). 通常, fgets给出一个以换行符结尾的字符串( char*\\n )。 This code snippet checks the input to see if the last character is a newline (indicating the length was OK) or another character (indicating it was too long and there is extra data in the stream). 此代码段检查输入内容,以查看最后一个字符是换行符(指示长度是确定的)还是其他字符(指示它太长并且流中有额外的数据)。

You can have scanf discard all chars after the first 10 this way 您可以用scanf以这种方式丢弃前10个字符之后的所有字符

scanf("%10s%*s", firstName);

The %*s in this case means scan a string but don't store it to a variable. 在这种情况下, %*s表示扫描字符串,但不将其存储到变量中。 Mind you that in this case scanf will stop scanning on any whitespace char. 请注意,在这种情况下scanf将停止对任何空白char进行扫描。 If the strings you enter have spaces that will cause a problem 如果您输入的字符串中的空格会引起问题

You need to clear the contents of the standard input stream. 您需要清除标准输入流的内容。 This can be done changing these: 可以通过更改以下内容来完成:

scanf("%10s", firstName);
scanf("%20s", familyName);

to

scanf("%10s%*[^\n]", firstName);scanf("%*c");
scanf("%20s%*[^\n]", familyName);scanf("%*c");

The %*[^\\n] intructs scanf to scan and discard everything until a newline character ( '\\n' ). %*[^\\n]指示scanf扫描并丢弃所有内容,直到换行符( '\\n' )。 %*c instructs scanf to scan and discard a character which, in this case, is probably the newline character. %*c指示scanf扫描并丢弃一个字符,在这种情况下,该字符可能是换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM