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 编辑 ]