为什么与fgets不输入第一个值? [英] Why fgets is not inputting first value?

查看:174
本文介绍了为什么与fgets不输入第一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序来快速写我的HTML文件。而当我来到写我页面的内容我有一个问题。

I am writing a program to write my html files rapidly. And when I came to write the content of my page I got a problem.

#include<stdio.h>

int main()
{
int track;
int question_no;

printf("\nHow many questions?\t");

scanf("%d",&question_no);

char question[question_no][100];

    for(track=1;track<=question_no;track++)
    {
        printf("\n<div class=\"question\">%d. ",track);
        printf("\nQuestion number %d.\t",track);
        fgets(question[track-1],sizeof(question[track-1]),stdin);
        printf("\n\n\tQ%d. %s </div>",track,question[track-1]);
    }

}

在这个程序我写的一些问题进行解答(在HTML文件)。当我试运行这个程序,我输入 question_no 的价值为3。但是,当我进入我的第一个问题,它不会在的问题[0去] ,因此第一个问题不输出。问题的输入没有问题的其余部分。

In this program I am writing some questions and their answers (in html file). When I test run this program I input the value of question_no to 3. But when I enter my first question it doesn't go in question[0] and consequently the first question doesn't output. The rest of the questions input without issue.

我搜索了计算器的一些问题,发现与fgets()查找最近的 \\ 0 字符和 \\ 0 停止。

我还发现,我应该使用缓存以输入以及通过与fgets()所以我用: setvbuf用来则setbuf 但也没有工作(我可能有codeD是错误的)。我还用 fflush(标准输入)后,我的第一个和最后一个(以及) scanf函数语句删除任何 \\从标准输入0 字符,但也没有工作。

I searched some questions on stackoverflow and found that fgets() looks for last \0 character and that \0 stops it.
I also found that I should use buffer to input well through fgets() so I used: setvbuf and setbuf but that also didn't work (I may have coded that wrong). I also used fflush(stdin) after my first and last (as well) scanf statement to remove any \0 character from stdin but that also didn't work.

有什么办法接受与fgets()
第一个输入
我使用stdin和stdout现在。我不访问,读取或写入的任何文件。

推荐答案

使用与fgets 的第一个提示了。你也应该的malloc 您的数组因为你不知道它是要去多久在编译的时候。

Use fgets for the first prompt too. You should also malloc your array as you don't know how long it is going to be at compile time.

#include <stdlib.h>
#include <stdio.h>

#define BUFSIZE 8

int main()
{
    int track, i;
    int question_no;
    char buffer[BUFSIZE], **question;

    printf("\nHow many questions?\t");

    fgets(buffer, BUFSIZE, stdin);
    question_no = strtol(buffer, NULL, 10);

    question = malloc(question_no * sizeof (char*));
    if (question == NULL) {
        return EXIT_FAILURE;
    }
    for (i = 0; i < question_no; ++i) {
        question[i] = malloc(100 * sizeof (char));
        if (question[i] == NULL) {
            return EXIT_FAILURE;
        }
    }

    for(track=1;track<=question_no;track++)
    {
        printf("\n<div class=\"question\">%d. ",track);
        printf("\nQuestion number %d.\t",track);
        fgets(question[track-1],100,stdin);
        printf("\n\n\tQ%d. %s </div>",track,question[track-1]);
    }

    for (i = 0; i < question_no; ++i) free(question[i]);
    free(question);
    return EXIT_SUCCESS;
}

用C

二维数组

的二维数组键入可以重新通过指针数组psented $ P $到键入或等效键入** (指针的指针的键入)。这需要两个步骤。

2D arrays in C

A 2D array of type can be represented by an array of pointers to type, or equivalently type** (pointer to pointer to type). This requires two steps.

使用的char **问题作为典范:

第一步是分配的char * 的数组。 的malloc 返回指向分配给它的,或 NULL 如果它失败了内存的开始。因此,检查问题 NULL

The first step is to allocate an array of char*. malloc returns a pointer to the start of the memory it has allocated, or NULL if it has failed. So check whether question is NULL.

二是使每个这些的char * 指向自己的字符的数组。因此,循环分配的数组100 字符 S上的大小设置为问题中的每个元素。同样,每个的malloc S需要返回 NULL ,所以你应该检查这一点。

Second is to make each of these char* point to their own array of char. So the for loop allocates an array the size of 100 chars to each element of question. Again, each of these mallocs could return NULL so you should check for that.

的malloc 值得免费 所以你应该反向时执行的过程您在使用已分配的内存完成。

Every malloc deserves a free so you should perform the process in reverse when you have finished using the memory you have allocated.

的malloc参考

长整型与strtol(为const char * str中,焦炭** endptr,INT基地);

与strtol 返回长整型(这在code以上浇注到 INT )。它分裂 STR 分为三个部分:

strtol returns a long int (which in the code above is casted to an int). It splits str into three parts:


  1. 任何空白preceding字符串的数字内容

  2. 它识别为数字,它会尝试转换部分

  3. 字符串的其余部分

如果 endptr NULL ,它将指向第三部分,让你知道在哪里与strtol 结束。你可以使用这样的:

If endptr is not NULL, it will point to the 3rd part, so you know where strtol finished. You could use it like this:

#include <stdio.h>                                                                                                                             
#include <stdlib.h>                                                                                                                            

int main()                                                                                                                                     
{                                                                                                                                              
    char * endptr = NULL, *str = "    123some more stuff";                                                                                       
    int number = strtol(str, &endptr, 10);                                                                                                       
    printf("number interpreted as %d\n"                                                                                                          
           "rest of string: %s\n", number, endptr);                                                                                              
    return EXIT_SUCCESS;                                                                                                                         
}

输出:

number interpreted as 123
rest of string: some more stuff

strtol将

这篇关于为什么与fgets不输入第一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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