C - 函数没有执行?

时间:2015-03-27 16:35:50

标签: c

1.和3.菜单项有效,但如果我选择2.菜单项,则表示我的数据没有列出。这是为什么? (它必须在linux命令行中编译并运行[NO IDE!])

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

struct log{
    int     id;
    char    name[20];
    char    location[20];
    int     quantity;
    char    quantity_type[10];
    time_t  added;
};

char fname[] = "data2.dat";

void add();
void listAll();

int nextId();

int main(){
    int ch;

    while(1){
        system("clear");

        printf("///Log stuff!///\n\n");

        printf("1. Add\n");
        printf("2. List all\n");            
        printf("0. Exit\n");

        printf("\n///////////////////////////////\n\n");

        printf("Choose a number from the menu: ");
        scanf("%d", &ch);

        switch(ch){
            case 1: add();
            break;

            case 2: listAll();
            break;

            case 0: exit(0);
            break;
        }    
    }   

    return 0;
}

void add(){
    FILE *f;
    struct log log;

    f = fopen(fname, "ab");
    if(f == NULL){perror("Can't open!\n");}

    log.id = nextId();
    printf("\nName: ");
    scanf("%s", log.name);
    printf("\nLocation: ");
    scanf("%s", log.location);
    printf("\nQuantity: ");
    scanf("%d", &log.quantity);
    printf("\nQuantity type: ");
    scanf("%s", log.quantity_type);
    log.added = time(NULL);

    fwrite(&log,sizeof(log),1,f);

    fclose(f);
}

void listAll(){
    FILE *f;
    struct log log;

    f = fopen(fname,"rb");
    if(f == NULL){perror("Can't open!\n");exit(1);}

    printf("\n///////////////////////////////\n\n");        
    printf("\tList of all data\n\n");
    printf("///////////////////////////////\n\n");

    while(fread(&log,sizeof(log),1,f) == 1){        
        printf("%d\t",log.id);
        printf("%s\t",log.name);
        printf("%s\t",log.location);
        printf("%d\t",log.quantity);
        printf("%s\t",log.quantity_type);
        printf("%s",ctime(&log.added));
    }
    printf("\n///////////////////////////////\n\n");

    fclose(f);
}

int nextId(){
    FILE *f;
    struct log log;
    int id = 1;

    f = fopen(fname, "rb"); 
    if(f == NULL){perror("Can't open!\n");}

    while(!feof(f)){
        fread(&log, sizeof(log), 1, f);
        if(log.id >= id){id = log.id + 1;}
    }

    fclose(f);

    return id;
}
  • 有更优雅的方式来完成这项任务吗?

1 个答案:

答案 0 :(得分:0)

该代码的菜单显示/输入选择/切换部分存在逻辑错误 在用户有机会查看任何显示的数据之前,屏幕正在被清除。

正确放置对系统(clear)和getchar()的调用将解决问题 一个函数应该只有1个正常退出。任何其他出口点应仅用于错误条件

您可以尝试以下方法:

 system(clear);

 int done = 0;
 while( !done )
 {
     printf("///Log stuff!///\n\n");

     printf("1. Add\n");
     printf("2. List all\n");
     printf("3. Exit\n");

     printf("\n///////////////////////////////\n\n");

     printf("Choose a number from the menu: ");
     if( 1 != scanf("%d", &ch) )
     { // then scanf failed
         perror( "scanf for menu choice failed" );
         exit( EXIT_FAILURE );
     }

     // implied else, scanf successful

     // eliminate menu from screen
     system(clear);

     switch(ch)
     {
        case 1:
            add();
        break;

        case 2:
            listAll();
            printf( "\npress any key to continue\n" );
            getchar();
        break;

        case 3:
            done=1;
        break;

        default:
            printf("\nyou entered an invalid menu selection, try again\n");
            break;
     } // end switch
}   // end while