2015-07-06 159 views
0

我正在尝试写电话簿程序。我完成了第一个功能(这节省了知识)。程序正在运行。但是,在我通过键盘输入该人的知识并关闭该程序后,它会向我显示最后一个人的文件知识。我正在使用程序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); 
} 

回答

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 - 附加;打开或创建文件进行更新,在文件结尾处写入。
2

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

0

如果在打开写模式(“W”或“W +”),最后的文本保存在文件中,但如果你将打开附加模式(“A”或“+”),你的文件将包括你所有的写作。

0

那么在上述程序 -

  1. 最前一页计划总是进入你enetres最后一个记录你在“W”模式打开的文件和你再次调用函数void newRecord()来然后再进入另一条记录从而关闭文件打开它。由于已经包含要在其之后的下一条记录的数据,但是在“w”模式下打开文件时,它会丢弃文件中已有的数据,并将该文件视为新的空文件。

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

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

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); 
}