| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2166 人关注过本帖
标题:GCC 编译器 如何刷新标准输入(stdin)缓冲区,我用fflush(stdin)刷新,gets() ...
取消只看楼主 加入收藏
oszc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:111
专家分:154
注 册:2011-4-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
GCC 编译器 如何刷新标准输入(stdin)缓冲区,我用fflush(stdin)刷新,gets()函数无法输入!
如题,vs2010可以编译成功,但是gcc有问题
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void inputData (FILE *fPtr);
void updateData (FILE *fPtr);
void delData (FILE *fPtr);
void printData (FILE *fPtr);
int exist ( int num,FILE *fPtr);  /*determine if the rec exist*/
int choice (void);

struct hardWareData 
{
    int recNum;
    char toolName[30];
    int toolNum;
    double toolCost;
};

int main ( void )
{
    int choose;
    FILE *fPtr;

    if( (fPtr=fopen("client.dat","rb++"))==NULL)
        printf("File not exist\n");
    else
    {
        while ( (choose=choice() )!=5)
        {
            switch (choose)
            {
                case 1:
                inputData(fPtr);
                break;
                case 2:
                updateData(fPtr);
                break;
                case 3:
                delData(fPtr);
                break;
                case 4:
                printData(fPtr);
                break;
                default:
                printf("Please input correct number!\n\n");
            }
        }
        fclose(fPtr);
    }
}

int choice(void)
{
    int choose;
    printf("\n1 - Input New Data in \"client.dat\".\n"
           "2 - Update record\n"
           "3 - Delete record\n"
           "4 - Print records\n"
           "5 - Exit\n? ");

    scanf ("%d",&choose);
    return choose;
}

void inputData(FILE *fPtr)
{
    int newRec;
    struct hardWareData hardWare={ 0, "", 0, 0.00};
    char a[100]={0};
    
    printf("Enter New Record Number:\n? ");
    scanf("%d", &newRec);
    
    if( exist(newRec,fPtr) )
    {
        printf("The record has already exist.Use \"Update Record\"\n");
    }
    else
    {
        hardWare.recNum=newRec;
        
        printf("\n***Enter toolName toolNum toolCost:\n? ");
        fflush(stdin);   // <--------------------------------------------------------------------------------只有这部分重要!!!
        gets(a);
        sscanf(a,"%[^0-9]%d%lf",hardWare.toolName, &hardWare.toolNum, &hardWare.toolCost);
        
        fseek(fPtr, (newRec-1) * sizeof( struct hardWareData), SEEK_SET);
        fwrite( &hardWare, sizeof( struct hardWareData), 1, fPtr);
    }
    printf("\n");
}
void updateData(FILE *fPtr)
{
    int upNum;
    struct hardWareData hardWare= {0, "", 0, 0.00};
    char a[100];

    printf("Enter Update Recourd Number:\n? ");
    scanf("%d", &upNum);

    if(!exist(upNum,fPtr))
    {
        printf("The record do not exist.Use \"Input Data\"\n");
    }
    else
    {
        fseek( fPtr,(upNum - 1) *sizeof (struct hardWareData), SEEK_SET);
        fread( &hardWare,sizeof(struct hardWareData),1,fPtr);
        printf("The current data is :/n");
        hardWare.recNum = upNum;
        printf("\n%-5d%-15s%-8d%.2lf\n\n",hardWare.recNum,hardWare.toolName,hardWare.toolNum,hardWare.toolCost);
        printf("Enter New ToolName TollNum ToolCost:\n? ");
    
        fflush(stdin);
        gets(a);
        sscanf(a,"%[^0-9]%d%lf",hardWare.toolName, &hardWare.toolNum, &hardWare.toolCost);

        fseek( fPtr,(upNum - 1) *sizeof (struct hardWareData), SEEK_SET);
        fwrite( &hardWare,sizeof(struct hardWareData),1,fPtr);
    }
    printf("\n");
}

void delData(FILE *fPtr)
{
    int delNum;
    struct hardWareData empty={0,"",0,0.00};

    printf("Enter the record to delete.\n? ");
    scanf("%d", &delNum);
    
    if(!exist(delNum,fPtr))
    {
        printf("Record do not exist!\n");
    }
    else
    {
        fseek(fPtr,(delNum-1) * sizeof (struct hardWareData), SEEK_SET);
        fwrite(&empty, sizeof(struct hardWareData),1,fPtr);
    }

}
void printData(FILE *fPtr)
{
    struct hardWareData H={0,"",0,0.00};
    int verify=0;

    rewind(fPtr);

    printf("\n\n*****************************************\n");
    while (!feof(fPtr))
    {    
        fread( &H,sizeof(struct hardWareData), 1 , fPtr);
        if(verify!=H.recNum && H.recNum !=0)
        {
            printf("%-5d%-15s%-8d%.2lf\n",H.recNum,H.toolName,H.toolNum,H.toolCost);
            verify=H.recNum;
        }
    }
    printf("*****************************************\n\n");
    }
int exist (int num,FILE *fPtr)
{
    struct hardWareData hardWare={0,"",0,0.00};

    fseek(fPtr, (num - 1) * sizeof( struct hardWareData), SEEK_SET);

    fread(&hardWare, sizeof(struct hardWareData), 1, fPtr);

    if (hardWare.recNum == 0)
        return 0;
    else 
        return 1;
}


运行程序要在文件夹下面 建立一个client.dat
在菜单下面选择 1:增加记录, 然后就跳过了,应该这时候有个gets(a)函数;而且已经fflush(stdin),无效。
同样 update 记录也是,BTW在win下运行良好。


[ 本帖最后由 oszc 于 2011-6-6 21:24 编辑 ]
搜索更多相关主题的帖子: 编译器 缓冲区 color 
2011-06-06 21:06
oszc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:111
专家分:154
注 册:2011-4-15
收藏
得分:0 
已上
2011-06-06 21:25
oszc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:111
专家分:154
注 册:2011-4-15
收藏
得分:0 
各位我在网上搜索以后,终于找到原因了。 标准C的fflush()支持刷新输出缓冲区,对于输入缓冲区无定义。

网址在下面,给出一个比较简单的解决方案,有同样问题的同学可以去看看。
http://bbs.
2011-06-06 23:20
快速回复:GCC 编译器 如何刷新标准输入(stdin)缓冲区,我用fflush(stdin)刷新,g ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037836 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved