2

我可以导入文件并阅读它,但我认为它不会保存到结构中。我想弄清楚如何将它保存在结构中,以便我可以搜索/删除联系人。不幸的是,冗长的代码如下:

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define BUFFSIZE 500
//Structure for contacts. These are now pointers.
typedef struct friends_contact{

  char *First_Name;
  char *Last_Name;
  char *home;
  char *cell;
}fr;
//Function declarations 
void menu(fr*friends ,int* counter,int i,char buffer[]);
void setFirst(fr*,int *,int i,char buffer[]);
char getFirst(fr*,int i);
void setLast(fr*friends, int* counter, int i,char buffer[]);
char getLast(fr*friends , int i);
void setHome(fr*friends, int* counter, int i,char buffer[]);
char getHome(fr*friends, int i);
void setCell(fr*friends, int* counter, int i,char buffer[]);
char getCell(fr*friends, int i);
void add_contact(fr*friends,int* counter,int i,char buffer[]);
void print_contact(fr*friends ,int* counter, int i,char user_entry3[50]);
char delete_contact(fr*friends ,int* counter, int i);
int show_contact(fr*friends ,int* counter, int i);
void file(fr*friends ,int* counter, int i,char user_entry3[50]);
void file2 (fr*friends ,int* counter, int i,char buffer[],FILE*read);

int main() 
{
  fr friends[5];
  char buffer[BUFFSIZE];
  int counter=0;
  int i=0;

  menu(friends, &counter,i,buffer);

  getch();
  return 0;
}
//Menu function
void menu(fr*friends,int* counter, int i,char buffer[]) 
{
 int user_entry=0;
 int user_entry1=0;
 int user_entry2=0;
 char user_entry3[50]={'\0'};
 FILE *read;
 printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
 scanf("%d",&user_entry1);
 if(user_entry1==1)
   {
    printf("Please enter a file name");
    scanf("%s",user_entry3); 
    read=fopen(user_entry3,"r");
   }else;

 do{
  int result;

  printf("\nPhone Book Application\n");
  printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show  phonebook\n5)Exit\n");   
  scanf("%d", &user_entry);
if(user_entry==1)
  {
    add_contact(friends,counter,i,buffer);
  }
if(user_entry==2)
  {
    delete_contact(friends ,counter,i);
  } 
if(user_entry==3)
  {
    result=show_contact(friends ,counter,i);
    if(result==0){
                  printf("\nName not Found\n");
                  }else{
                        result;
                        }

 }                  
if(user_entry==4)
 {
   print_contact(friends, counter,i,user_entry3);
   if(user_entry1==1)
     {
   file2(friends ,counter,i,buffer,read);
      }else;
 } 

}while(user_entry!=5);
  if(user_entry==5)
    {
      printf("Would you like to save entries to a file? (1)yes or (2) no");
      scanf("%d",&user_entry2);
      if(user_entry2 == 1)
        {
          printf("Please name your file");
          scanf("%s",user_entry3); 
          file(friends, counter,i,user_entry3);
          printf("Goodbye!"); 

     }else if(user_entry2 == 2)
        {
     printf("Goodbye!"); 
        }

    }

 }
//Start of Set functions. Each entry has its own set function that gathers the data
//For each interaction, we have a buffer and a malloc.
void setFirst(fr*friends, int* counter, int i,char buffer[]) 
{
  printf("Enter a first name \n");
  scanf("%s",buffer);

  friends[*counter].First_Name=malloc(BUFFSIZE*strlen(buffer));
  strcpy(friends[*counter].First_Name, buffer);

}

void setLast(fr*friends, int* counter, int i,char buffer[]) 
{

  printf("Enter a last name \n");
  scanf("%s",buffer);

  friends[*counter].Last_Name=malloc(BUFFSIZE*strlen(buffer));
  strcpy(friends[*counter].Last_Name, buffer);
}
void setHome(fr*friends, int* counter, int i,char buffer[]) 
{

  printf("Enter a home number \n");
  scanf("%s",buffer);

  friends[*counter].home=malloc(BUFFSIZE*strlen(buffer));
  strcpy(friends[*counter].home, buffer);
}
void setCell(fr*friends, int* counter, int i,char buffer[]) 
{

  printf("Enter a cell number \n");
  scanf("%s",buffer);

  friends[*counter].cell=malloc(BUFFSIZE*strlen(buffer));
  strcpy(friends[*counter].cell, buffer);
}
//Start of Get functions. Each function sends the data to the executing function.
char getFirst(fr*friends , int pos) 
{

   printf("%s ", friends[pos].First_Name);
   return *friends[pos].First_Name;
}

char getLast(fr*friends , int pos)
{

   printf("%s\n", friends[pos].Last_Name);
   return *friends[pos].Last_Name;

}

char getHome(fr*friends , int pos) 
{

   printf("(Home) ""%s\n", friends[pos].home);
   return *friends[pos].home;
}

char getCell(fr*friends , int pos) 
{

   printf("(Cell) ""%s\n", friends[pos].cell);
   return *friends[pos].cell;
}
//This function allows for the all the set functions to be added.
void add_contact(fr*friends,int* counter,int i,char buffer[]) 
{
   setFirst(friends,counter,i,buffer); 
   setLast(friends,counter,i,buffer);
   setHome(friends,counter,i,buffer);
   setCell(friends,counter,i,buffer);
   (*counter)++;
}

//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter, int i)
{
   char name_search[50]={'\0'};
   char Delete[5]={'\0'};

   printf("Search by last name\n");
   scanf("%s",name_search);//Name entry
   for(i=0;i<*counter;i++)
      {
       if(strcmp(name_search,friends[i].Last_Name)==0)//Copys over the name entered
         {                                       
          strcpy(friends[i].Last_Name,Delete);
         }
       }
    //Freeing up memory.
    free(friends[i].First_Name);
    free(friends[i].Last_Name);
    free(friends[i].home);
    free(friends[i].cell);   

    printf("\nName(s) has been deleted\n");             
}
//This function prints out all the contact information
void print_contact(fr*friends ,int* counter, int i,char user_entry3[50]) 
{
    for( i = 0; i < *counter; i++)
    if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&&        strlen(friends[i].home)&& strlen(friends[i].cell ))
       {

          getFirst(friends, i);
          getLast(friends, i);
          getHome(friends, i);
          getCell(friends, i);
        }
}
//Displays the contact in which you are searching for.
int show_contact(fr*friends ,int* counter, int i) 
{  
   char name_search2[50]={'\0'};
   int flag=0;
   printf("Please enter a last name\n");
   scanf("%s",name_search2);
   for(i=0;i<*counter;i++)
      {
      //If the name is found, it reaturns the contact info.Now works for duplicate last names.
        if(strcmp(name_search2,friends[i].Last_Name)==0)
          {
             (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&& strlen(friends[i].home)&& strlen(friends[i].cell ));

            getFirst(friends, i);
            getLast(friends, i);
            getHome(friends, i);
            getCell(friends, i);
            flag++;
          }
     }         


    return flag;
}    
void file(fr*friends ,int* counter, int i,char user_entry3[5])
{
FILE*fp;
char flag2;
fp=fopen(user_entry3,"w");

for( i = 0; i < *counter; i++)
   {
     if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&&    strlen(friends[i].home)&& strlen(friends[i].cell ))
        {

        fprintf(fp,"\n""%s ",friends[i].First_Name);
        fprintf(fp,"%s ""\n",friends[i].Last_Name);
        fprintf(fp,"<Home>""%s""\n",friends[i].home);
        fprintf(fp,"<Cell>""%s""\n",friends[i].cell);

        }
  }
   fclose(fp);
}

void file2(fr*friends ,int* counter, int i,char buffer[],FILE*read)
{
  fseek(read, 0, SEEK_SET); 

  while (fscanf(read,"%s", buffer) != EOF) 
     {
       friends[*counter].Last_Name=malloc(BUFFSIZE*strlen(buffer));

       strcpy(friends[*counter].Last_Name, buffer);

       printf("%s\n",friends[*counter].Last_Name);

      }

}

我知道代码并不完美,并且可能有一些与这个问题无关的缺陷,但我已经尝试了一段时间来解决这个问题。如果有人可以帮助我,将不胜感激!

4

2 回答 2

2

您正在将文件读入函数 file2 中的朋友结构中,但仅当输入的选项为 4 时才调用 file2 - 在以下代码段中。

if(user_entry==4)
 {
   print_contact(friends, counter,i,user_entry3);
   if(user_entry1==1)
     {
   file2(friends ,counter,i,buffer,read);
      }else;
 }

在搜索或删除(user_entry = 2 或 user_entry = 3)的情况下,文件永远不会被读入结构,因此会出现问题。对于这些情况,您还需要确保调用 file2。

于 2012-11-18T07:44:43.173 回答
0

r+您可以在模式下打开文件。如果您的代码需要在内存中的数据和磁盘中的数据之间进行同步,您可以通过在此模式下打开文件并根据需要更新文件来实现,例如当用户完成输入时。

于 2012-11-18T07:35:24.670 回答