简体   繁体   English

fgets()不能像我期望的那样工作

[英]fgets() does not work as I expect it to

Can anyone tell me why this code does not work? 任何人都可以告诉我为什么这段代码不起作用? When i run, it just prints out "Enter info about trail 1" and without any input, skips to another step. 当我运行时,它只打印出“输入关于路径1的信息”并且没有任何输入,跳到另一步。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 15

void readingArrays(int numberOfTrails,char arr[MAX][20]);

char array[MAX][20];

int main(void)
{
    int numberOfTrails;
    printf("Enter the number of trails\n");
    scanf("%d",&numberOfTrails);

    readingArrays(numberOfTrails,array);

    return 0;
}

void readingArrays(int numberOfTrails,char arr[numberOfTrails][20])
{
    for(int i=0;i<numberOfTrails;i++)
    {
        printf("Enter info about trails %d\n",i+1);
        fgets(arr[i],4,stdin);
        //getchar();
        //strtok(arr[i], "\n");
        printf("%s\n",arr[i]);
    }
}

Issue 问题

The scanf function in main reads the integer, but leaves the newline \\n on the input stream. main中的scanf函数读取整数,但在输入流上留下换行符\\n When the first loop iteration of readingArrays comes around, fgets sees this newline and assumes you have already entered your trail info and pressed enter. readingArrays的第一次循环迭代出现时, fgets看到这个换行符,并假设您已经输入了跟踪信息并按下回车键。 It prints the empty string and newline, and goes on to the next iteration. 它打印空字符串和换行符,然后继续下一次迭代。

Solution

One solution would be to tell scanf to read and ignore whitespace after the digit by adding a space after %d format specifier. 一种解决方案是通过在%d格式说明符之后添加空格来告诉scanf读取并忽略数字后面的空格。

So to get numberOfTrails from stdin, you would do 所以要从stdin获取numberOfTrails ,你会这样做

scanf("%d ",&numberOfTrails);

Thanks to @Ray for pointing out this solution! 感谢@Ray指出这个解决方案!

The perl language gives you chomp, which removes the newline from lines read from input. perl语言为你提供了chomp,它从输入中读取的行中删除了换行符。 I find it a useful function to have lying around, 我发现躺在身边很有用,

char*
chomp(char* p)
{
    if(!p) return p;
    size_t len=strlen(p);
    //if(len<=0) return(p);
    if(len>0) len--;
    if(p[len]=='\n') p[len--]='\0';
    if(len>=0) if(p[len]=='\r') p[len--]='\0';
    return(p);
}

So, declare a variable to use to read a line, and then just use gets to parse the line. 因此,声明一个用于读取一行的变量,然后使用gets来解析该行。 Loads clearer. 负载更清晰。 And notice that your array is an array of MAX arrays of char[20], and you may well enter a far larger line. 请注意,您的数组是一个MAX [@]数组的数组,您可能会输入更大的行。 It can be effective to read the entire line entered, and then extract the part you want, 读取输入的整行,然后提取所需的部分是有效的,

char array[MAX][20];
int ndx;
char line[100];
for( ndx=0; fgets(line,sizeof(line)-1),stdin); ++ndx ) {
    chomp(line);
    strncpy(array[ndx],line,sizeof(array[ndx])-1);
    //combine both into one line:
    //strncpy(array[ndx],chomp(line),sizeof(array[ndx])-1);
    /*NULL termination of array[ndx] left as exercise for reader*/
}

1) Add this line after scanf() : getchar() 1)在scanf()之后添加这一行:getchar()

2) Change fgets (...) 4 to 19. 2)改变fgets(...)4到19。

The program will work. 该计划将有效。

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

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