fgets() Not Ignoring New Line

旧巷老猫 提交于 2019-12-23 06:28:28

问题


For my practice assignment I have to use either gets() or fgets(). I chose fgets() as its more secure.

The first input is meant to be able to hold a maximum of 5 characters. So i gave the char array a size of 6 to accommodate the trailing '\0'.

I found the fgets() issue of it adding a trailing '\n' when you press Enter (using stdin with fgets())

I done a bit of research and found a for loop to try and get rid of it. However, it doesnt seem to be working and i cant for the life of me figure out why.

Its still skipping the next input when i type in 5 characters.

Here is the code:

#include <stdio.h>
#include <string.h>

int main(void)
{
    //Declare char arrays
    char    arcInputString5[6];
    char    arcInputString10[11];
    char    arcInputString15[16];
    char    arcInputString20[21];
    int     clean1, clean2, clean3, clean4;
//  int     nStrLen1, nStrLen2, nStrLen3, nStrLen4;
//  nStrLen1 = nStrLen2 = nStrLen3 = nStrLen4 = 0;

printf("\nPlease Input String 1 - Max Length 5: ");
//gets(arcInputString5);
fgets(arcInputString5, 6, stdin);

for(clean1 = 0; clean1 < strlen(arcInputString5); clean1++)
{
    if(arcInputString5[clean1] == '\n' || arcInputString5[clean1] == '\r')
    {
        arcInputString5[clean1] = '\0';
        break;
    }
}

printf("\nPlease Input String 2 - Max Length 10: ");
//gets(arcInputString10);
fgets(arcInputString10, 10, stdin);

printf("\nPlease Input String 3 - Max Length 15: ");
//gets(arcInputString15);
fgets(arcInputString15, 15, stdin);

printf("\nPlease Input String 4 - Max Length 20: ");
//gets(arcInputString20);
fgets(arcInputString20, 20, stdin);

printf("\nThankyou For Your Inputs - They Are Shown Back To You Below\n");

puts(arcInputString5);
puts(arcInputString10);
puts(arcInputString15);
puts(arcInputString20);

printf("\nThe String Lengths For Each Input Are Listed Below");

printf("\n%d", strlen(arcInputString5));
printf("\n%d", strlen(arcInputString10));
printf("\n%d", strlen(arcInputString15));
printf("\n%d", strlen(arcInputString20));

}

Ive tried multiple ways of doing the for loop such as using the number 6 instead of "strlen(arcInputString5)"

Any help would be appreciated.

EDIT:

EXAMPLE INPUT:

asd d

EXAMPLE OUTPUT:

Please Input String 2 - Max Length 10: //skips this
Please Input String 3 - Max Length 15: //this is the next line for input

回答1:


fgets() reads one character less than the given buffer size from stdin and then appends a NUL-character. So in your case, with an input buffer of 6 characters, it reads "asd d" into arcInputString5, and the newline character that terminates the line input is still unread.

The next fgets() then reads (only) this newline character into arcInputString10.

You need a buffer size of (at least) 7 to read the five characters "asd d" including the newline character from stdin.

The same applies to your other buffers used for fgets().

Added: As Jonathan Leffler correctly commented, a better method is to supply a "large" input buffer to fgets() and check the actual length of the user input after reading one line.

You should also note that fgets() returns NULL if no character could be read at all (end-of-file), so you should check the return value.




回答2:


Change 6 to 7:
arcInputString5[7];
fgets(arcInputString5, 7, stdin);

You need to give space for the '\n' and '\0' characters.

valter




回答3:


Call getchar() to retrieve the the newline character from the input stream before asking for the next user input.

I like to use the following simple function for clearing the input stream

void clear() {
    while(getchar() != '\n');
}


来源:https://stackoverflow.com/questions/21270323/fgets-not-ignoring-new-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!