在C中使用.txt文件和电话簿程序的任务

时间:2013-04-23 01:33:59

标签: c text-files

下面是我在C中的电话簿程序的代码。我在修改它时遇到了问题。我需要修改它,以便在将条目添加到电话簿后,内容将被写入.txt文件。删除条目后,我需要将内容写入相同的.txt文件并打印全部内容。我应该使用fopen()函数吗?我是初学者......

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

typedef struct Phonebook_Contacts
   {
      char FirstName[20]; 
      char LastName[20];
      char PhoneNumber[20]; 
   } phone; 


void AddEntry(phone * ); 
void DeleteEntry(phone * ); 
void PrintEntry(phone * );
void SearchForNumber(phone * ); 
void RandomName(phone * ); 
void DeleteAll(phone * ); 
void FreeContacts (phone * ); 


int counter = 0; 
char FileName[256]; 
FILE *pRead; 
FILE *pWrite; 


int main (void)
   {      
      phone *phonebook; 
      phonebook = (phone*) malloc(sizeof(phone)*100); 
      int iSelection = 0; 


         if (phonebook == NULL)
         {

         printf("Out of Memory. The program will now exit");
         return 1;
         }
         else {} 

      do
      {
         printf("\n\t\t\tPhonebook Menu");
         printf("\n\n\t(1)\tAdd Friend");
         printf("\n\t(2)\tDelete Friend");
         printf("\n\t(3)\tDisplay Phonebook Entries");
         printf("\n\t(4)\tSearch for Phone Number");
         printf("\n\t(5)\tFind a Random Friend");
         printf("\n\t(6)\tDelete All Entries");
         printf("\n\t(7)\tExit Phonebook");
         printf("\n\nWhat would you like to do? ");
         scanf("%d", &iSelection);  


         if (iSelection == 1)
         {
            AddEntry(phonebook); 
         } 



         if (iSelection == 2)
         {
            DeleteEntry(phonebook); 
         } 


         if (iSelection == 3)
         {
            PrintEntry(phonebook); 
         } 



         if (iSelection == 4)
         {
            SearchForNumber(phonebook);         
         } 


         if (iSelection == 5)
         {
            RandomName(phonebook);         
         } 


         if (iSelection == 6)
         {
            DeleteAll(phonebook); 
         } 


         if (iSelection == 7)
         {
            printf("\nYou have chosen to exit the Phonebook.\n");
            system("pause");
            FreeContacts(phonebook);
            return 0;
         } 
      } while (iSelection <= 9); 
   } 


void AddEntry (phone * phonebook)
{  
   pWrite = fopen("phonebook_contacts.dat", "a");
   if ( pWrite == NULL )
   {
      perror("The following error occurred ");
      exit(EXIT_FAILURE); 
   }
      else
      {
         counter++; 
         realloc(phonebook, sizeof(phone)); 

         printf("\nFirst Name: ");
         scanf("%s", phonebook[counter-1].FirstName);
         printf("Last Name: ");
         scanf("%s", phonebook[counter-1].LastName);
         printf("Phone Number (XXX-XXX-XXXX): "); 
         scanf("%s", phonebook[counter-1].PhoneNumber);
         printf("\n\tFriend successfully added to Phonebook\n"); 

         fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);
         fclose(pWrite); 
      } 
} 

void DeleteEntry (phone * phonebook)
{
   int x = 0;
   int i = 0;
   char deleteFirstName[20];  // 
   char deleteLastName[20];  

      printf("\nFirst name: ");
      scanf("%s", deleteFirstName);
      printf("Last name: ");
      scanf("%s", deleteLastName);

      for (x = 0; x < counter; x++)
      {
         if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0) 
         {
            if (strcmp(deleteLastName, phonebook[x].LastName) == 0) 
            {
                for ( i = x; i < counter - 1; i++ )
               {
                  strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName); 
                  strcpy(phonebook[i].LastName, phonebook[i+1].LastName); 
                  strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber); 
               } 
               printf("Record deleted from the phonebook.\n\n");
               --counter; 
               return;
            } 
         } 
      }    

   printf("That contact was not found, please try again.");
}

void PrintEntry (phone * phonebook)
{
   int x = 0;

   printf("\nPhonebook Entries:\n\n ");
   pRead = fopen("phonebook_contacts.dat", "r");
   if ( pRead == NULL)
   {
      perror("The following error occurred: ");
      exit(EXIT_FAILURE);
   }
   else
   {
      for( x = 0; x < counter; x++) 
      {
         printf("\n(%d)\n", x+1); 
         printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName); 
         printf("Number: %s\n", phonebook[x].PhoneNumber); 
      } 
   } 
   fclose(pRead);
} 


void SortByFirstName (phone * phonebook)
{
   int i = 0;
   int x = 0;
   int swap;
   int TempCounter = counter;
   phone Temp; 

   do
   {
      swap = 0;
      for(i = 1; i < TempCounter; i++)
      {

         if(strcmp(phonebook[i-1].FirstName, phonebook[i].FirstName) > 0)
         {
            Temp = phonebook[i]; 
            phonebook[i] = phonebook[i-1]; 
            phonebook[i-1] = Temp; 

            strcpy(Temp.FirstName, phonebook[i].FirstName);
            strcpy(Temp.LastName, phonebook[i].LastName);
            strcpy(Temp.PhoneNumber, phonebook[i].PhoneNumber);

            swap = 1;
         }
      } 
      TempCounter--;
   } while (swap); 

   printf("\nYour friends in Alphabetical Order by First Name:\n\n");
   for( x = 0; x < counter; x++ )
   {
      printf("\n(%d)\n", x+1); 
      printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName); 
      printf("Number: %s\n", phonebook[x].PhoneNumber); 
   }   
} 


void SortByLastName (phone * phonebook)
   {
   int i = 0;
   int x = 0;
   int swap;
   int TempCounter = counter;
   phone Temp; 

   do
   {
      swap = 0;
      for(i = 1; i < TempCounter; i++)
      {

         if(strcmp(phonebook[i-1].LastName, phonebook[i].LastName) > 0)
         {
            Temp = phonebook[i]; 
            phonebook[i] = phonebook[i-1]; 
            phonebook[i-1] = Temp;

            strcpy(Temp.FirstName, phonebook[i].FirstName);
            strcpy(Temp.LastName, phonebook[i].LastName);
            strcpy(Temp.PhoneNumber, phonebook[i].PhoneNumber);

            swap = 1;
         } 
      } 
      TempCounter--;
   } while (swap); 

   printf("\nYour friends in Alphabetical Order by First Name:\n\n");
   for( x = 0; x < counter; x++ )
   {
      printf("\n(%d)\n", x+1); 
      printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName); 
      printf("Number: %s\n", phonebook[x].PhoneNumber); 
   }                 
} 


void SearchForNumber (phone * phonebook)
{
   int x = 0;
   char TempFirstName[20]; 
   char TempLastName[20]; 

   printf("\nPlease type the name of the friend you wish to find a number 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].FirstName) == 0) 
      {
         if (strcmp(TempLastName, phonebook[x].LastName) == 0) 
         {

            printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName, phonebook[x].LastName, phonebook[x].PhoneNumber);
         } 
      } 
   }     
} 


void RandomName (phone * phonebook)
{
   int iRandom = 0;
   srand(time(NULL));
   iRandom = rand() % counter;  
   int x = iRandom; 

   printf("\nYour random friend is: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
   printf("Their phone number is: %s\n", phonebook[x].PhoneNumber);
}    


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

   for ( x = 0; x < counter; x++ )
   {
      strcpy(phonebook[x].FirstName, nullStr); 
      strcpy(phonebook[x].LastName, nullStr); 
      strcpy(phonebook[x].PhoneNumber, nullStr); 
      --counter; 
   }   

   printf("All Contacts have been deleted.\n");     
} 
void FreeContacts (phone * phonebook)
{
     --counter;
     for ( ; counter > 0; --counter)
     {
        free(phonebook[counter].FirstName); 
        free(phonebook[counter].LastName); 
        free(phonebook[counter].PhoneNumber); 
        free(phonebook); 
        counter = 0; 
        return; 
     }
} 

2 个答案:

答案 0 :(得分:1)

确定你想做“realloc(电话簿,sizeof(电话));”在你的AddEntry()函数?这几乎肯定不是你想象的那样。

答案 1 :(得分:0)

您需要的所有代码几乎已经在此程序中。我建议你从查阅教程或阅读文件I / O章节开始。另外一个资源是C函数的手册页。如果您使用的是* NIX系统,则可以键入man fopen,也可以在Google中输入该内容,以便在Windows系统上获得相同的结果。

Here's the man page for fopen()。如您所见,有两个参数,您要打开的文件的名称和模式。

显然,可以选择打开文件进行写入以及附加已存在的文件。

您提到要写入.txt文件的要求,在您的代码中打开.dat文件phonebook_contacts.dat这是您正在讨论的文件吗?

最简单的选择之一是将整个文件读入内存然后在那里更改并将其写回文件(重写现有文件),但它并不高效,因为你必须遍历每个现有元素并将它们复制到您的phonebook变量中。您可能需要咨询教师,看看这是否是可接受的解决方案。类似的东西:

 while( more to read){
    read fname line
    strcpy(phonebook[i].first_name, fname)
    read lname line
    strcpy(phonebook[i].last_name, fname)
    read num line
    strcpy(phonebook[i].number, num)
    increment i
 }