>我正在尝试读取一个txt文件,然后将其保存到C语言中的链表中。我已经尝试了很多,但我没有找到解决方案。
这是我所拥有的:
typedef struct contact{
char name[50];
char number[10];
char bithday[15];
struct contact *next;
}contact;
struct contact *head;
FILE *fp;
这部分将我已经拥有的联系人保存到txt文件中。
void backup(){
struct contact *ap;
fp=fopen("save_contacts.txt", "w");
if(fp==NULL){
printf(" Errorn");
system("pause");
return 1;
}
for (ap=head;ap!=NULL;ap=ap->next){
fprintf(fp,"%s ",ap->name);
//fprintf(fp,", ");
fprintf(fp,"%s ",ap->number);
//fprintf(fp,", ");
fprintf(fp,"%s ",ap->birthday);
//fprintf(fp,", ");
fprintf(fp,"; ");
}
fprintf(fp, "End");
}
所以我的问题出在这部分,它不读取 txt 文件。
void read() {
struct contact *ap;
int i;
head=NULL;
char aux[30];
FILE *fp=fopen("save_contacts.txt","r");
do{
head=NULL;
ap=(contact*)malloc(sizeof(contact));
fscanf(fp,"%s ",&ap->name);
fscanf(fp,"%s ",&ap->number);
fscanf(fp,"%s ",&ap->birthday);
fscanf(fp,"%s ",aux);
if(strcmp(aux,";")==0){
ap=ap->next;
}
}while(strcmp(aux,"End")!=0);
}
正在执行
ap=(contact*)malloc(sizeof(contact)); fscanf(fp,"%s ",&ap->name); fscanf(fp,"%s ",&ap->number); fscanf(fp,"%s ",&ap->birthday); fscanf(fp,"%s ",aux); if(strcmp(aux,";")==0){ ap=ap->next;
未设置ap->next
,因此第二轮 ap 具有未定义的值,因此当您取消引用它时行为未定义
请注意您的保存方式:
for (ap=head;ap!=NULL;ap=ap->next){ ... fprintf(fp,"; "); } fprintf(fp, "End");
与您的阅读方式不兼容,其中最后一个元素的";"被"End">替换
你可以这样做(我想最后一个元素没有';'(:
int read() {
FILE *fp=fopen("save_contacts.txt","r");
if (fp == NULL) {
fprintf(stderr, "cannot open 'save_contacts.txt'n");
head = NULL;
return 0;
}
contact ** pp = &head;
char aux[30];
int result;
for (;;) {
*pp = malloc(sizeof(contact));
if (fscanf(fp,"%49s %9s %14s %29s", (*pp)->name, (*pp)->number, (*pp)->birthday, aux) != 4) {
fputs("invalid file, cannot read the 3 fields then ;/Endn", stderr);
result = 0;
free(*pp);
break;
}
pp = &(*pp)->next;
if (strcmp(aux,"End") == 0) {
result = 1;
break;
}
if (strcmp(aux, ";") != 0) {
fputs("invalid file, expected ; or Endn", stderr);
result = 0;
break;
}
}
*pp = NULL;
fclose(fp);
return result;
}
出现问题时返回 0,否则返回 1
注意我保护字符串的读取不写出它们,并检查文件是否正确
例如,拥有完整的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct contact{
char name[50];
char number[10];
char birthday[15]; /* not bithday */
struct contact *next;
}contact;
struct contact *head;
int read() {
FILE *fp=fopen("save_contacts.txt","r");
if (fp == NULL) {
fprintf(stderr, "cannot open 'save_contacts.txt'n");
head = NULL;
return 0;
}
contact ** pp = &head;
char aux[30];
int result;
for (;;) {
*pp = malloc(sizeof(contact));
if (fscanf(fp,"%49s %9s %14s %29s", (*pp)->name, (*pp)->number, (*pp)->birthday, aux) != 4) {
fputs("invalid file, cannot read the 3 fields then ;/Endn", stderr);
result = 0;
free(*pp);
break;
}
pp = &(*pp)->next;
if (strcmp(aux,"End") == 0) {
result = 1;
break;
}
if (strcmp(aux, ";") != 0) {
fputs("invalid file, expected ; or Endn", stderr);
result = 0;
break;
}
}
*pp = NULL;
fclose(fp);
return result;
}
int main()
{
if (read()) {
/* debug */
for (contact * p = head; p != NULL; p = p->next)
printf("%s %s %sn", p->name, p->number, p->birthday);
}
/* free resources */
while (head != NULL) {
contact * p = head;
head = head->next;
free(p);
}
return 0;
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall l.c
pi@raspberrypi:/tmp $ cat save_contacts.txt
bruno 007 19/02/1960 ;
you 001 01/01/2010 ;
bar 123 31/12/1999 End
pi@raspberrypi:/tmp $ ./a.out
bruno 007 19/02/1960
you 001 01/01/2010
bar 123 31/12/1999
pi@raspberrypi:/tmp $
瓦尔格林德的处决:
pi@raspberrypi:/tmp $ valgrind ./a.out
==2334== Memcheck, a memory error detector
==2334== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2334== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2334== Command: ./a.out
==2334==
bruno 007 19/02/1960
you 001 01/01/2010
bar 123 31/12/1999
==2334==
==2334== HEAP SUMMARY:
==2334== in use at exit: 0 bytes in 0 blocks
==2334== total heap usage: 6 allocs, 6 frees, 5,712 bytes allocated
==2334==
==2334== All heap blocks were freed -- no leaks are possible
==2334==
==2334== For counts of detected and suppressed errors, rerun with: -v
==2334== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $