为什么不将文件数据保存到结构中?

时间:2012-11-19 05:53:09

标签: c file pointers struct malloc

我不明白我在这里做错了什么。我有一个成功从文件中读取数据的程序。一旦它这样做,应该能够搜索/显示/删除在应该读取的数据,但它不会这样做。我知道文件正在被成功读取,因为当我向读取文件的函数添加printf()时,它会打印出正确的数据。帮助会很精彩。这是代码的精简版。

typedef struct friends_contact{

  char *First_Name;
  char *Last_Name;
  char *home;
  char *cell;
}fr;


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


 }

删除功能

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

      }

}

1 个答案:

答案 0 :(得分:0)

尝试将您的读取功能更改为这样的位置,您传递friends数组的大小,这样就不会读取太多行并最终导致程序崩溃。假设您正在从文本文件中逐行阅读。

这将循环,而我们的counter小于friends数组中的元素数,以确保我们在访问每个结构时不会超出界限,或者如果我们到达了文件结束,然后就会停止。

要为Last_Name分配内存,我们将获取存储在buffer中的字符串的长度,并添加1以便为空终止字节腾出空间。

void file2(fr *friends, const size_t num_friends, FILE *read)
{
    size_t counter = 0;
    char buffer[128];

    while (counter < num_friends && fscanf(read, "%s", buffer) != EOF) {
        friends[counter].Last_Name = malloc(strlen(buffer) + 1);
        strcpy(friends[counter].Last_Name, buffer);
        counter++;
    }
}

然后调用它像file2(friends, 5, read);中间参数是数组的大小。

如果您为fscanf提供过多数据,

buffer也可能会溢出fgets。它没有边界检查,可能导致程序崩溃。我会考虑下次使用{{1}}。