文件中的数据保存问题

时间:2015-07-06 11:22:00

标签: c

我正在尝试编写电话簿程序。我完成了第一个功能(可以节省知识)。程序正在运行。然而,在我从键盘输入人的知识并关闭程序后,它向我展示了最后一个人的文件知识。我正在使用程序w模式,我也尝试了w +模式。但问题仍然没有改变。例如,当我使用第一个函数时,我输入了三个人的知识,但它只显示了一个。我该如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>     // "stdlib" library contains of exit() and malloc function
#include <Windows.h>   // "Windows" library contains of Sleep() function which waits the system as you want

struct personKnowledge
{
    char number[16];
    char name[16];
    char surname[16];
};

void newRecord();
void display();
void deletE();
void add();
void update();

FILE *ptrFILE;

int main()
{
    int choice;
    do
    {
        printf("\n\t\t *-* Phone Book Program *-*");
        printf("\n\n\t\t 1) New record");   // The options are being presented to user
        printf("\n\n\t\t 2) Display person knowledge");
        printf("\n\n\t\t 3) Delete someone");
        printf("\n\n\t\t 4) Add new person");
        printf("\n\n\t\t 5) Update person knowledge");
        printf("\n\n\t\t 6) Exit");
        printf("\n\n\nEnter your choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
        {
            newRecord();
            break;
        }
        case 2:
        {
            break;
        }
        case 3:
        {
            break;
        }
        case 4:
        {
            break;
        }
        case 5:
        {
            break;
        }
        case 6:
        {
            printf("\nWorking has been completed.\n");
            exit(EXIT_SUCCESS);
            break;
        }
        default:
        {
            printf("\nWrong entry! The program has been terminated.\n");
            break;
        }
        }
    } while (choice >= 1 && choice <= 6);
    return 0;
}

void newRecord()
{
    system("cls");   // Screen is being cleaned
    if ((ptrFILE = fopen("Phone Book.txt", "w")) == NULL)
    {
        printf("The file couldn't open\n");
    }
    else
    {
        struct personKnowledge *p;   // p means person
        p = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));   // Memory is being allocated
        fflush(stdin);
        printf("\n\nDetermine person name: ");   // User is entering the person's knowledge and they are being saved in file
        gets(p->name);
        printf("Determine %s's surname: ", p->name);
        gets(p->surname);
        printf("Determine %s's number: ", p->name);
        gets(p->number);
        fprintf(ptrFILE, "Name\t\t\t\tSurname\t\t\t\tNumber\n");
        fprintf(ptrFILE, "--------\t\t   ----------------\t\t------------------------\n");
        fprintf(ptrFILE, "\n%s%33s%38s\n", p->name, p->surname, p->number);
        free(p);
        printf("Please wait, information is saving to file..\n");
        Sleep(750);
        printf("*-* Saving operation has been completed. *-*");
    }
    fclose(ptrFILE);
}

5 个答案:

答案 0 :(得分:3)

您正在使用写入模式将输出写入现有文件。要保留其内容,请使用aa+等附加模式。例如

FILE * ptrFILE = fopen("Phone Book.txt", "a");

description其他模式(可能与所有平台不兼容):

  1. rrb - 打开文件进行阅读。
  2. wwb - 截断为零长度或创建文件进行书写。
  3. aab - 附加;打开或创建文件以便在文件结尾处写入。
  4. r+rb+r+b - 打开文件进行更新(阅读和撰写)。
  5. w+wb+w+b - 截断为零长度或创建文件以供更新。
  6. a+ab+a+b - 附加;打开或创建文件以进行更新,在文件末尾写入。

答案 1 :(得分:2)

您应该以追加模式打开它。试试这个:
if ((ptrFILE = fopen("Phone Book.txt", "a")) == NULL)
附加模式会将内容添加到现有文件内容的末尾。

答案 2 :(得分:0)

如果您以写入模式(“w”或“w +”)打开,最后一个文本将保存在文件中,但如果您将以追加模式(“a”或“a +”)打开,您的文件将包含所有文本你的写作。

答案 3 :(得分:0)

上面提到的程序 -

  1. Fisrt程序始终输入您在“w”模式下打开文件时所记录的最后一条记录,并再次调用函数void newRecord()以输入另一条记录,从而关闭文件然后再次打开它。由于已经包含了您希望下一个记录的数据,但是当文件以“w”模式打开时,它会丢弃文件中已存在的数据,并将该文件视为新的空文件。

  2. 在你的答案中提到的第二个程序只是主要功能,因此不会一次又一次地打开和关闭文件。这就是为什么它通常会保存您的数据。

  3. 使用“a”或“a +”模式让您的第一个程序正常工作。

答案 4 :(得分:0)

无需a或+模式。我坚持:)这是新的代码。问题已经解决了。

#include <stdio.h>
#include <stdlib.h>     // "stdlib" library contains of exit() and malloc function
#include <Windows.h>   // "Windows" library contains of Sleep() function which waits the system as you want

struct personKnowledge
{
    char number[16];
    char name[16];
    char surname[16];
};

void newRecord(FILE *);///
void display();
void deletE();
void add();
void update();

FILE *ptrFILE;

int main()
{
    int choice;
    if ((ptrFILE = fopen("Phone Book.txt", "w+")) == NULL)
    {
        printf("The file couldn't open\n");
    }
    do
    {
        printf("\n\t\t *-* Phone Book Program *-*");
        printf("\n\n\t\t 1) New record");   // The options are being presented to user
        printf("\n\n\t\t 2) Display person knowledge");
        printf("\n\n\t\t 3) Delete someone");
        printf("\n\n\t\t 4) Add new person");
        printf("\n\n\t\t 5) Update person knowledge");
        printf("\n\n\t\t 6) Exit");
        printf("\n\n\nEnter your choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
        {
            newRecord(ptrFILE);
            break;
        }
        case 2:
        {
            break;
        }
        case 3:
        {
            break;
        }
        case 4:
        {
            break;
        }
        case 5:
        {
            break;
        }
        case 6:
        {
            printf("\nWorking has been completed.\n");
            exit(EXIT_SUCCESS);
            break;
        }
        default:
        {
            printf("\nWrong entry! The program has been terminated.\n");
        }
        }
    } while (choice >= 1 && choice <= 6);
    fclose(ptrFILE);
    return 0;
}

void newRecord(FILE *ptrFILE)
{
    static int counter = 0;
    system("cls");   // Screen is being cleaned
    struct personKnowledge *p;   // p means person
    p = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));   // Memory is being allocated
    fflush(stdin);
    printf("\n\nDetermine person name: ");   // User is entering the person's knowledge and they are being saved in file
    gets(p->name);
    printf("Determine %s's surname: ", p->name);
    gets(p->surname);
    printf("Determine %s's number: ", p->name);
    gets(p->number);
    if (counter == 0)
    {
        fprintf(ptrFILE, "Name\t\t\t\tSurname\t\t\t\tNumber\n");
        fprintf(ptrFILE, "--------\t\t   ----------------\t\t------------------------\n");
    }   
    fprintf(ptrFILE, "\n%33s%33s%38s\n", p->name, p->surname, p->number);
    printf("Please wait, information is saving to file..\n");
    Sleep(750);
    printf("*-* Saving operation has been completed. *-*");
    counter++;
    free(p);
}