C语言怎么从文件中将信息导入链表中

编写一个通讯录管理系统。addressbook.txt中按姓名顺序存储联系人信息,联系人信息包括姓名、单位、住宅电话、手机、电子邮件、通讯地址。本系统应完成以下几方面的功能:
(1)导入信息:从addressbook.txt中的联系人信息导入链表中。
(2)输入新联系人:从键盘输入新的联系人,并将它插入到链表的相应位置。
(3)显示信息:显示所有通讯录里的条目。
(4)查询:在文件中根据姓名查询出联系人的信息
(5)存盘:将链表中的信息写入addressbook.txt中。

这个实现起来挺简单的,就是写起来麻烦点,不是一点代码能写完的!
1,建立一个链表,链表的节点struct定义为联系人信息的格式;
2,读取文件,把内容存入链表;
3,查询就根据姓名关键字遍历链表就行了;
4,把内容存入文件;

首先建立链表,以及插入节点,查询链表函数写出来;
文件的读取和存入到不是很麻烦;
----------------------------下面是简单的实现,可以输入,存入文件,从文件读取,打印,如果还想要实现其他的稍修改一下就行了------

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

#define MAX 80

struct stu{
int id;
char name[MAX];
struct stu* next;
};

typedef struct stu STU;
typedef STU* STUPTR;

STUPTR insert(STUPTR head, int id, char* name);
void print_st(STUPTR head);
void save_to_file(FILE* fp, STUPTR head);
FILE* crt_file( void );
STUPTR read_to_link(STUPTR head);

int main( void )
{

int choice;
int stu_id;
char stu_name[MAX];

STUPTR head;

FILE* fp;

clrscr();
head = NULL;

clrscr();

printf( "please enter the choice!\n" );
scanf( "%d", &choice );

while( choice != 9 ){

switch(choice){
case 1: printf("enter the insert ----id---name!\n");
scanf( "%d%s", &stu_id ,stu_name);
head = insert(head,stu_id,stu_name);
break;

case 2:print_st(head);
break;

case 3:puts("save the info to file e:\stu_info.txt!\n");
fp = crt_file();
save_to_file( fp, head);
puts( "save the data sucessful!\n");
fclose(fp);
break;

case 4:puts("read the file to link!\n");
head = NULL;
head = read_to_link(head);
puts("read the file successful!\n");
break;

}

printf( "please enter the choice!\n");
scanf( "%d", &choice );
}

}

STUPTR insert(STUPTR head, int id, char* name)
{
STUPTR new, cur;

cur = head;

new = malloc( sizeof( STU) );

new->id = id;
strcpy(new->name, name);
new->next = NULL;

if(cur == NULL)
head = new;
else{
while(cur->next != NULL){
cur = cur->next;
}
cur->next = new;
}

return head;
}

void print_st(STUPTR head)
{
STUPTR cur=head;
int i;
i=1;

if(cur == NULL){
printf( "has no student info!\n" );
}
else while(cur != NULL){
printf( "%d:------%d---%s\n", i,cur->id,cur->name);
i++;
cur = cur->next;

}

}

void save_to_file(FILE* fp, STUPTR head)
{
int i;
STUPTR cur;

cur = head;

while(cur != NULL){
fprintf(fp, "%d %s\n", cur->id,cur->name);
cur = cur->next;
}
}

FILE* crt_file(void)
{
FILE* fp;
fp = fopen( "e:\stu_info.txt", "w" ); /*w is right or not*/
if(fp == NULL)
puts("shit!!!!!!!!!!");
return fp;
}

STUPTR read_to_link( STUPTR head)
{

int id;
char name[MAX];

FILE* fp;
fp = fopen("e:\stu_info.txt", "r");

while( !feof(fp) ){
fscanf(fp, "%d%s", &id, name );
head = insert(head, id, name);
}

return head;

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-29

一、操作步骤:
1,建立一个链表,链表的节点struct定义为联系人信息的格式;
2,读取文件,把内容存入链表;
3,查询就根据姓名关键字遍历链表就行了;
4,把内容存入文件;
二、例程:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 80
struct stu{
  int id;
  char name[MAX];
  struct stu* next;
};
typedef struct stu STU;
typedef STU*       STUPTR;
STUPTR insert(STUPTR head, int id, char* name);
void print_st(STUPTR head);
void save_to_file(FILE* fp, STUPTR head);
FILE* crt_file( void );
STUPTR  read_to_link(STUPTR head);
int main( void )
{
  
  int choice;
  int stu_id;
  char stu_name[MAX];
  STUPTR head;
  FILE* fp;
  clrscr();
  head = NULL;
clrscr();
  printf( "please enter the choice!\n" );
  scanf( "%d", &choice );
  while( choice != 9 ){
      switch(choice){
         case 1: printf("enter the insert ----id---name!\n");
                 scanf( "%d%s", &stu_id ,stu_name);
                 head = insert(head,stu_id,stu_name);
                 break;
         case 2:print_st(head);
                break;
         case 3:puts("save the info to file e:\stu_info.txt!\n");
                fp = crt_file();
                save_to_file( fp,  head);
                puts( "save the data sucessful!\n");
                fclose(fp);
                break;
         case 4:puts("read the file to link!\n");
                head = NULL;
                head = read_to_link(head);
                puts("read the file successful!\n");
                break;
                
      }
      printf( "please enter the choice!\n");
      scanf( "%d", &choice );
  }
}
STUPTR  insert(STUPTR head, int id, char* name)
{
   STUPTR new, cur;
   cur = head;
   new = malloc( sizeof( STU) );
   new->id = id;
   strcpy(new->name, name);
   new->next = NULL;
   if(cur == NULL)
    head = new;
    else{
          while(cur->next != NULL){
              cur = cur->next;
          }
          cur->next = new;
    }
    return head;
}
void print_st(STUPTR head)
{
    STUPTR cur=head;
    int i;
    i=1;
    
    if(cur == NULL){
      printf( "has no student info!\n" );
    }
    else while(cur != NULL){
        printf( "%d:------%d---%s\n", i,cur->id,cur->name);
        i++;
        cur = cur->next;
    }
}
void save_to_file(FILE* fp, STUPTR head)
{
     int i;
     STUPTR cur;
     cur = head;
     while(cur != NULL){
       fprintf(fp, "%d     %s\n", cur->id,cur->name);
       cur = cur->next;
     }
}
FILE* crt_file(void)
{
  FILE* fp;
  fp = fopen( "e:\stu_info.txt", "w" );   /*w is right or not*/
  if(fp == NULL)
    puts("shit!!!!!!!!!!");
  return fp;
}
STUPTR read_to_link( STUPTR head)
{
      
      int id;
      char name[MAX];
      FILE* fp;
      fp = fopen("e:\stu_info.txt", "r");
while( !feof(fp) ){
      fscanf(fp, "%d%s", &id, name );
      head = insert(head, id, name);
      }
      
      return head;
      
}

相似回答