2015-07-06 84 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); 
}