与fgets()没有忽视新线 [英] fgets() Not Ignoring New Line

查看:136
本文介绍了与fgets()没有忽视新线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的实践任务,我必须为使用gets()函数或与fgets()。
我选择了与fgets()作为其更安全。

第一输入是为了能够最多5个字符保​​持。
所以我给char数组的大小为6,以适应尾随'\\ 0'。

我找到了与fgets()的问题,它增加了尾部的'\\ n',当你preSS输入(使用与fgets标准输入())

我做了一些调查,发现了一个for循环,试图摆脱它。但是,它似乎是工作,我不能为我的生活弄清楚为什么。

它仍然跳过下一个输入,当我在5个字符类型。

下面是code:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    //声明字符数组
    炭arcInputString5 [6];
    焦炭arcInputString10 [11];
    炭arcInputString15 [16];
    焦炭arcInputString20 [21];
    INT clean1,clean2,clean3,clean4;
// INT nStrLen1,nStrLen2,nStrLen3,nStrLen4;
// nStrLen1 = nStrLen2 = nStrLen3 = nStrLen4 = 0;的printf(\\ n请输入字符串1 - 最大长度5:);
//得到(arcInputString5);
与fgets(arcInputString5,6,标准输入);为(clean1 = 0; clean1&下;的strlen(arcInputString5); clean1 ++)
{
    如果(arcInputString5 [clean1] =='\\ n'|| arcInputString5 [clean1] =='\\ r')
    {
        arcInputString5 [clean1] ='\\ 0';
        打破;
    }
}的printf(\\ n请输入字符串2 - 最大长度10);
//得到(arcInputString10);
与fgets(arcInputString10,10,标准输入);的printf(\\ n请输入字符串3 - 最大长度15);
//得到(arcInputString15);
与fgets(arcInputString15,15,标准输入);的printf(\\ n请输入字符串4 - 最大长度20);
//得到(arcInputString20);
与fgets(arcInputString20,20,标准输入);的printf(\\ nThankyou为您的输入 - 它们被显示给您以下\\ n);看跌期权(arcInputString5);
看跌期权(arcInputString10);
看跌期权(arcInputString15);
看跌期权(arcInputString20);的printf(\\ n此字符串长度对于每个输入下面列出了);的printf(\\ N%D的strlen(arcInputString5));
的printf(\\ N%D的strlen(arcInputString10));
的printf(\\ N%D的strlen(arcInputString15));
的printf(\\ N%D的strlen(arcInputString20));

}

香港专业教育学院尝试过做for循环的多种方式,如使用,而不是strlen的(arcInputString5)

6号

任何帮助将是AP preciated。

编辑:

输入例:

  ASDð

输出示例:

 请输入字符串2  - 最大长度10://跳过此
请输入字符串3 - 最大长度15://这是输入下一行


解决方案

与fgets()读的一个字符少的不是从给定的缓冲区大小标准输入,然后
追加NUL字符。所以你的情况,用6个字符的输入缓冲器,
它读取ASD D到 arcInputString5 ,并终止线路输入的换行符仍然未读。

接下来与fgets()然后读取头(只)这个换行符到 arcInputString10

您需要(至少)7缓冲区大小来读取五个大字ASD D,包括
换行字符从标准输入。

这同样适用于你的使用与fgets()

其他缓冲区

补充:的乔纳森莱弗勒正确评价,更好的方法是提供
一个大输入缓冲器与fgets()并检查用户输入的实际长度后
读一行。

您还应该注意到,与fgets()返回NULL,如果没有字符可以被读取
(结束文件),所以你应该检查返回值。

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

解决方案

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.

这篇关于与fgets()没有忽视新线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆