【问题标题】:Saving data in files in c将数据保存在c中的文件中
【发布时间】:2018-12-07 15:43:43
【问题描述】:

我正在处理这个电影院预订项目!我在这里面临的两个问题是数据保存。

1):当我关闭程序并尝试注册新用户时,以前注册用户的登录信息会被删除即使我以“a+”模式打开文件。但是如果我直接使用已经注册的用户登录它就可以了。问题是我一次不能注册超过 1 个用户。

2) : 当我关闭程序并再次登录时,用户预订信息将被删除。我也想保存用户的预订信息。

我该如何解决这个问题?

struct login
    {
        char fname[100];
        char lname[100];
        char username[20];
        char password[20];
    };
    #include <stdio.h>
    #define RVALUE 5
    #define CVALUE 10
    int i, j;
    void DisplaySeats(void);
    void ReserveSeats(void);
    void ChooseSeat(void);
    void CancelSeat(void);
    void CheckCancelSeat(void);
    void menu(void);
    int Seats[RVALUE][CVALUE];

    void registration();
    void login();
    int main()
    {
        int c;
        int choice, menu;
        printf("Welcome to our small Cinema!!!\n");
        printf("\n");
        //DisplaySeats();
        printf("\n1 : register!!!\n");
        printf("2 : login!!!\n");
        printf("3 : quit\n");
        printf("Enter your choice : \n");
        scanf("%d",&c);
        switch (c)
        {
        case 1:
            registration();
            break;
        case 2:
            login();
            break;
        case 3:
            printf("Thankyou for Choosing our small Cinema !! \n");
            getch();
            exit(1);
        default :
            system("CLS");
            printf("Enter a valid number !!\n");
            main();
        }

        getch();

    }
    void registration()
    {
        FILE *log;
        log = fopen("login.txt", "a+");
        struct login l;
        printf("\nEnter first name : ");
        scanf("%s", &l.fname);
        printf("\nEnter last name : ");
        scanf("%s", &l.lname);
        printf("\nEnter your Username : ");
        scanf("%s", &l.username);
        printf("\nEnter your password : ");
        scanf("%s", &l.password);
        fwrite(&l, sizeof(l), 1, log);
        fclose(log);
        printf("\nYou are successfully registered!!");
        printf("\nYour UserId is %s and your password is %s", l.username, l.password);
        printf("\nNow login with your username and password!!");
        printf("\nPress any key to continue ...");
        getch();
        system("CLS");
        main();
    }
    void login()
    {
        char username[100];
        char password[100];
        FILE *log;
        struct login l;
        log = fopen("login.txt", "r");
        if (log == NULL)
        {
            printf("FILE NOT FOUND!!!");
            exit(1);
        }
        printf("\nUserID : ");
        scanf("%s", &username);
        printf("\nPassword : ");
        scanf("%s", &password);
        while (fread(&l, sizeof(l), 1, log));
        {
            if (strcmp(username, l.username) == 0 && strcmp(password, l.password)==0)
            {
                system("CLS");
                printf("\nYou are successfully logged in !!\n");
                menu();

            }



            else
            {
                    printf("\nYour UserID or password is incorrect !!\n");
                    printf("Press any key to continue ...\n");
                    getch();
                    system("CLS");
                    login();
            }


        }
        fclose(log);

    }
    void ChooseSeat(void)
    {

        int row, col,k;
        printf("Which row do you want to choose? : ");
        scanf("%d", &row);
        printf("Which seat do you want to select? : ");
        scanf("%d", &col);
        if (row > RVALUE || col > CVALUE)
        {
            printf("Wrong Entry !! Try again\n");
            ChooseSeat();
        }
        else if (Seats[row - 1][col - 1] != 0)
        {
            printf("Seat is already reserved try again !!\n");
            ChooseSeat();
        }
        else
        {
            Seats[row - 1][col - 1] = 1;
            printf("Congratulations!! Reservation Completed!!!\n");
            DisplaySeats();
            printf("\nPress any key to go to main menu!!");
            getch();
            system("CLS");
            main();
        }


    }

    void ReserveSeats(void)
    {
        int NoOfSeats,i;
        printf("How many seats do you want to reserve?\n");
        scanf("%d", &NoOfSeats);
        DisplaySeats();
        for (i = 1; i <= NoOfSeats; i++)
        {
            ChooseSeat();
        }


    }
    void DisplaySeats(void)
    {
        printf("\t \t Seats\n");
        printf("\t 1 2 3 4 5 6 7 8 9 10\n");

        for (i = 0; i < RVALUE; i++)
        {
            printf("Rows %d : ", i + 1);
            for (j = 0; j < CVALUE; j++)
                printf("%d ", Seats[i][j]);
            printf("\n");
        }
        printf("\n");

    }
    void CheckCancelSeat(void)
    {
        while (1)
        {
            for (i = 0; i < RVALUE; i++)
            {
                for (j = 0; j < CVALUE; j++)
                {
                    if (Seats[i][j] == 0)
                    {
                        printf("There is no reserved seat available!!\n");
                        break;
                    }
                    else
                    {
                        CancelSeat();
                    }
                    break;
                }
                break;
            }
            system("CLS");
            break;


        }
    }
    void CancelSeat(void)
    {
        int r, c;
        printf("\nWhich row do you want to cancell ? : ");
        scanf("%d", &r);
        printf("\nWhich column do you want to cancell ? : \n");
        scanf("%d", &c);

        if (Seats[r - 1][c - 1] != 0)
        {
            Seats[r - 1][c - 1] = 0;
            printf("Your Seat is Cancelled !!\n");
            system("CLS");
        }
        else
        {
            printf("Wrong Entry !!\n");
            CancelSeat();
        }
    }
    void menu(void)
    {
        int choice;
            DisplaySeats();

            printf("1 : reserve a seat\n");
            printf("2 : cancell a seat\n");
            printf("3 : Main Menu\n");
            printf("Enter your choice : \n");
            scanf("%d", &choice);
            system("CLS");
            switch (choice)
                {
                    case 1:
                        ReserveSeats();
                        break;
                    case 2:
                        CheckCancelSeat();
                        break;

                    case 3:
                        main();
                        break;
                    default:
                        printf("Please enter a valid choice!!");
                        menu();
                        break;
                }
    }

【问题讨论】:

  • 代码太多,请缩减为minimal reproducible example
  • @Jean-FrançoisFabre 我不确定在哪里添加解决方案,这就是我放置整个代码的原因
  • @Jean-FrançoisFabre 你能帮我吗?请这是我必须提交的项目。

标签: c structure file-handling saving-data


【解决方案1】:

问题是很难将多个结构保存到一个二进制文件中。

注意:二进制文件没有文件扩展名“.txt”,而是 z。 “.Bin”、“.dat”等;有时二进制文件即使没有文件扩展名也会被保存。

我认为您尝试在二进制文件中“发送垃圾邮件”密码(加密)。不幸的是,我必须告诉你,这不起作用。如果您使用文本编辑器打开文件,您将看到密码。 我建议您将数据保存为文本文件,然后逐步读取数据。 此外,您没有声明所有功能。 有隐式声明。这可能会导致大型程序出错。对于少数人来说,解决方案是包含 stdlib.h。

如果您想在第二天晚上保存文件,请使用 Subfix "b" z 模式。例如“a + b”。 一些有用的链接是可能的:

http://www.cplusplus.com/reference/cstdio/fopen/ http://www.cplusplus.com/reference/cstdio/fwrite/ Reading/Writing a structure into a binary file

虽然没能解决问题,但还是希望能帮上忙。

如果您仍想将结构保存在二进制文件中,您可以在 Internet 上阅读此内容(我只是说 Google)。 例如,如果你想用 C 加密一些东西,你可以搜索一个 crypt library 或类似的东西。

【讨论】:

    猜你喜欢
    • 2012-05-07
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多