2014-03-06 57 views
0

典型的初學者電話簿程序,試圖添加讀寫文件功能。這不是編得很好,但在我執行的是函數7或8,我的ErrorHandler返回「無效的參數」無效的參數讀寫文件

編輯*整個事情更新的代碼,其中包括了一些修復:

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

typedef struct phonebook 
    { 
     char cFirstName[20]; 
     char cLastName[20]; 
     char PhoNo[20]; 
    } pb; 

//function prototypes 
void AddContact (pb *); 
void DeleteContact (pb *); 
void ShowContacts (pb *); 
void FindContact (pb *); 
void RandContact (pb *); 
void FindContact (pb *); 
void DeleteAll (pb *); 
void Read (pb *); 
void Write (pb *); 

char FileName[100]; 
FILE *pRead; 
FILE *pWrite; 

int counter = 0; 

main() 
    {  

    pb *phonebook; 
    phonebook = (pb*) malloc(sizeof(pb)*1); 
    int iChoice = 0; 

    while (iChoice <= 8) 
    { 
     printf("\n-Choose an option- \n"); 
     printf("\n\t(1)\tAdd Contact"); 
     printf("\n\t(2)\tDelete Contact"); 
     printf("\n\t(3)\tShow All Contacts"); 
     printf("\n\t(4)\tSearch for a Contact"); 
     printf("\n\t(5)\tRandom Contact"); 
     printf("\n\t(6)\tDelete All Contacts"); 
     printf("\n\n\t(7)\tWrite contacts to file"); 
     printf("\n\t(8)\tRead contacts from file"); 
     printf("\n\n\t(9)\tExit\n\n\t"); 

     scanf("%d", &iChoice); 

     if (iChoice == 1) 
     { 
      AddContact(phonebook); 
     } 

     if (iChoice == 2) 
     { 
      DeleteContact (phonebook); 
     } 

     if (iChoice == 3) 
     { 
      ShowContacts(phonebook); 
     } 
     if (iChoice == 4) 
     { 
      FindContact(phonebook); 
     } 
     if (iChoice == 5) 
     { 
      RandContact(phonebook); 
     } 
     if (iChoice == 6) 
     { 
      DeleteAll(phonebook); 
     } 
     if (iChoice == 7) 
     { 
      Write(phonebook); 
     } 
     if (iChoice == 8) 
     { 
      Read(phonebook); 
     } 
     if (iChoice == 9) 
     { 
      free(phonebook); 
      return 0; 
     }   
     } //end while 

    } //end main 


//function definitions 

    //add contact 
void AddContact (pb * phonebook) 
{ 
    counter++; //counter incremented for each entry 
    realloc(phonebook, sizeof(pb)); //realloc with every new contact 
     printf("\nFirst Name: "); 
     scanf("%s", phonebook[counter-1].cFirstName); 
     printf("Last Name: "); 
     scanf("%s", phonebook[counter-1].cLastName); 
     printf("Phone Number: "); 
     scanf("%s", phonebook[counter-1].PhoNo); 
     printf("\n\tContact added\n"); 
} 
    //delete contact 
void DeleteContact (pb * phonebook) 
{ 
    int x = 0; 
    char scrapcFirstName[20]; //strings for deleting original strings 
    char scrapcLastName[20]; 
    char nullStr[20] = {"\0"}; 

     printf("\nFirst name: "); 
     scanf("%s", scrapcFirstName); 
     printf("Last name: "); 
     scanf("%s", scrapcLastName); 
    //compare strings 
     for (x = 0; x < counter; x++) 
     { 
      if (strcmp(scrapcFirstName, phonebook[x].cFirstName) == 0) 
      { 
      for (x = 0; x < counter; x++) 
      { 
       if (strcmp(scrapcLastName, phonebook[x].cLastName) == 0) 
       { 
        strcpy(phonebook[x].cFirstName, nullStr); 
        strcpy(phonebook[x].cLastName, nullStr); 
        strcpy(phonebook[x].PhoNo, nullStr); 
       }//end if 
        else 
        { 
         printf("Invalid Input"); 
        } 
      }//end for 
      }//end if 
    } // end for 
    counter--; // Contact deleted, update counter 
     printf("Contact Deleted\n"); 
    } 

    // show phonebook 
void ShowContacts (pb * phonebook) 
    { 
     int x = 0; 
     printf("\nPhonebook:\n\n "); 
     for(x = 0; x < counter; x++) 
      { 
       printf("\n(%d)\n", x+1); 
       printf("Name: %s %s\n", phonebook[x].cFirstName, phonebook[x].cLastName); 
       printf("Number: %s\n", phonebook[x].PhoNo); 
      } //end for 
    } 

    //Find a specific contact 
void FindContact (pb * phonebook) 
{ 
    int x = 0; 
    char TempFirstName[20]; 
    char TempLastName[20]; 

    printf("\nWho are you looking for?"); 
    printf("\n\nFirst Name: "); 
    scanf("%s", TempFirstName); 
    printf("Last Name: "); 
    scanf("%s", TempLastName); 
    for (x = 0; x < counter; x++) 
    { 
     if (strcmp(TempFirstName, phonebook[x].cFirstName) == 0) 
     { 
     if (strcmp(TempLastName, phonebook[x].cLastName) == 0) 
     { 

      printf("\n%s %s \n%s\n", phonebook[x].cFirstName, phonebook[x].cLastName, phonebook[x].PhoNo); 
     } 
     } 
    }  
}  

    //show a random contact 
void RandContact (pb * phonebook) 
{ 
    int iRand = 0; 
    srand(time(NULL)); 
    iRand = rand() % counter; 
    int x = iRand; 

    printf("\n%s %s\n", phonebook[x].cFirstName, phonebook[x].cLastName); 
    printf("%s\n", phonebook[x].PhoNo); 
}  

    //delete all 
void DeleteAll (pb * phonebook) 
{ 
    int x = 0; 
    char nullStr[20] = {'\0'}; 

    for (x = 0; x < counter; x++) 
    { 
     strcpy(phonebook[x].cFirstName, nullStr); 
     strcpy(phonebook[x].cLastName, nullStr); 
     strcpy(phonebook[x].PhoNo, nullStr); 
     --counter; 
    } 

    printf("Contacts have been wiped.\n");  
} 

void Read(pb * phonebook) 
{ 
     FILE *pRead; 
     char name[256]; 

     printf("File to read: "); 
     gets(name); 
     pRead=fopen(name,"a"); 

     if(pRead != NULL) 
     { 
      printf("Contact List"); 
      while(!feof(pRead)){ 
       fread(phonebook, sizeof (struct phonebook), 1, pRead); 
       fclose(pRead); 
       if (!feof(pRead)){ 
       fread(phonebook, sizeof (struct phonebook), 1, pRead);  
       fclose(pRead);   
       } 
      } 
     } 
      else{ 

      goto ErrorHandler; 

      } 
    exit(EXIT_SUCCESS); 
    ErrorHandler: 
     perror("The following error occured"); 
     exit(EXIT_FAILURE); 
} 

void Write(pb * phonebook) 
{ 
     FILE *pWrite; 
     char name[256]; 

     printf("File to write:"); 
     gets(name); 
     pWrite=fopen(name,"a"); 

     if(pWrite != NULL) 
     { 
       fwrite(phonebook, sizeof (struct phonebook), 1, pRead); 
       fclose(pWrite); 
     } 
     else{ 

     goto ErrorHandler; 

     } 
    exit(EXIT_SUCCESS); 
    ErrorHandler: 
     perror("The following error occured"); 
     exit(EXIT_FAILURE); 
} 

它仍然給我同樣的錯誤,「無效的參數」

+0

在'Read()'中,完全混淆了正在使用哪個文件指針。另外,我沒有看到'fclose()','feof()'用得不好。 – unwind

+0

你是什麼意思?我能更好地使用feof?它正在等待執行條件,直到文件結束。 –

+0

這經常被提及,我應該尋找一個參考。在嘗試讀取之前,不能使用'feof()'檢測「文件結束」*,並且因爲您已到達文件結尾而無法檢測。它應該被用來弄清楚爲什麼失敗的東西沒有阻止它失敗。只讀,直到閱讀失敗。 – unwind

回答

0

未設置文件名:char name[256];然後fopen(name,"a")這就是爲什麼fopen失敗。 顯然得到()你scanf函數後不能正常工作,在49行糾正你的scanf:以scanf("%d\n" Input in C. Scanf before gets. Problem

的其他問題已經被指出(* PREAD而不是* PWRITE),不關閉文件等

+0

他正在從用戶讀取文件名。 –

0

在閱讀功能,您使用PWRITE代替PREAD

pWrite=fopen(name,"a"); 

,並檢查EOF條件PREAD

while(!feof(pRead)) 
+0

修正了這個問題,仍然給我同樣的錯誤,「無效論證」 –