带文件IO的链接列表
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<string>
struct link_list {
char mail[50];
int counter;
struct link_list *next;
};
typedef struct link_list node;
void main()
{
FILE *fp ;
char string1[80];
node *head=NULL;
int count_length=0;
char *fname = "email.txt";
fp = fopen ( fname, "r" ) ;
char line [ 128 ]; /* or other suitable maximum line size */
int count=0;
while ( fgets ( line, sizeof line, fp ) != NULL ) /* read a line */
{
count++;
if(head==NULL)
{
head=(node *)malloc(sizeof(node));
fscanf(fp,"%s",string1);
strcpy(head->mail,string1);
head->counter=count;
head->next=NULL;
}
else
{
node *tmp = (node *)malloc(sizeof (node));
fscanf(fp,"%s",string1);
strcpy(tmp->mail,string1);
tmp->next = head;
tmp->counter=count;
head = tmp;
}
}
fclose(fp); fp = fopen ( fname, "r" ) ;
fclose(fp);
//printf("%d",count_length);
getch();
}
程序在运行时会发出断言错误。有人可以帮我调试一下吗?
没有找到相关结果
已邀请:
2 个回复
摊揉售
循环的内部位可能应该用这条线做点什么?而不是使用fscanf从文件指针读取,如何使用“sscanf”从您刚刚读入的字符串中读取? 没有更多的背景,很难知道你想要做什么。如果你跳过空白行,那么我猜代码是有道理的。
响摔衅幸
永远不会被初始化。因此
将始终评估为false(这是未定义的行为,因为它未初始化) 除了Jeff Foster所说的,你正在调用
和
,它正在读取文件的一行,然后读取另一个字符串值。因此,如果
读取文件的最后一行,则您对
的调用将失败。这可能与您的断言失败有关。