【问题标题】:C - Unable read data from Input StreamC - 无法从输入流中读取数据
【发布时间】:2014-03-27 12:18:20
【问题描述】:

这是我用 C 编写的代码。

void BFS()
{
    int index[4][2];
    int p=10,i,j,x,y;
    int chld=-1;
    int cnt=1;
    int size=sizeof(NODE);
    char ch='n';
    qstart=NULL;
    lstart=NULL;
    NODE *ptr,*tmp;
    NODE* broot=(NODE*)malloc(size);
    NODE* bgoal=(NODE*)malloc(size);
    memcpy(broot,root,size);
    memcpy(bgoal,goal,size);
    push_queue(broot);


    //---------------------------------------------------------------
    //This scanf does not work well, don't read from input stream or console
    //---------------------------------------------------------------
    printf("Want to see the nodes\t[y/n]\n");
    scanf("%c",&ch);
    printf("\n\nTOTAL NODE COUNT ");
    //---------------------------------------------------------------



    while(p>0)
    {
        if((ptr=pop_queue())!=NULL)
        {
            positions(ptr,index,&x,&y);
            if(ch=='y' || ch=='Y')
            {
                printf("\t\t\tParent\n");
                print_node(ptr);
            }
            i=0;    j=0;    chld=-1;
            for(i=0;i<4;i++)
                if(index[i][0]!=-1)
                if((tmp=node_exchange(ptr,broot,index[i][0],index[i][1],x,y))!=NULL)
                {
                    printf("%d",cnt);
                    j=cnt;
                    cnt++;
                    tmp->parent=ptr;
                    ptr->child[++chld]=tmp;
                    if(!memcmp(tmp,bgoal,9*sizeof(int)))
                    {
                        trace_path(tmp);
                        printf("Total Nodes Skipped : %d",TOTAL_SKIP);
                        free_list();
                        return ;
                    }
                    push_queue(tmp);
                    push_list(tmp);
                    while(j>0)
                    {
                        j=j/10;
                        printf("\b");
                    }
                }
            if(ch=='y' || ch=='Y')
                print_child(ptr,chld);
            for(i=chld+1;i<4;i++)
                ptr->child[i]=NULL;
        }
        else{
            printf("WARNING: popped null\n");
            break;
        }
//      p--;
    }
    qstart=NULL;
}

这是我的程序的一个功能。 scanf 调用(由 cmets 在代码内标记)不会从控制台读取。之后它继续执行所有代码,并且不等待它应该做的输入。我正在使用 gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 和 gnome 终端。

示例输出为: 请进入初始状态: 初始节点

          0  1  2
          3  4  5
          6  7  8

请输入目标节点: 目标节点

          8  7  6
          5  4  3
          2  1  0

想查看节点 [y/n]

节点总数 885^C

相信我,我不知道为什么会这样进一步。

【问题讨论】:

  • 将scanf("%c",&amp;ch); 更改为scanf(" %c",&amp;ch);

标签: c linux gcc terminal scanf


【解决方案1】:

使用fgetc,像这样:

ch = fgetc(stdin);

这将从stdin 中读取一个字符。

【讨论】:

    【解决方案2】:

    您应该使用getch() 或getche()。
    我认为getche() 会更好,因为它与按下的字符相呼应。
    一般在程序中给出“是或否”提示时,默认为“否”时,“n”大写:[y/N]。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多