链表使用情况,以获得文件数据 [英] linked list usage to obtain data from file
本文介绍了链表使用情况,以获得文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有电子邮件地址present的文本文件。
i have a text file with emails addresses present.
我想获得这些电子邮件并将其存储在任何数据结构或变量。
然后,我需要从随机从数据结构中选择电子邮件地址。
i want to obtain those emails and store it in any data structure or variable. Then i need to select mail address from random from the data structure.
#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;
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();
}
我编辑了code..i我得到断言错误
I edited the code..i am getting assertion error
推荐答案
尝试添加新条目到列表中,而不是尾部的头部。例如:
Try adding new entries to the head of the list instead of the tail. For example:
node *tmp = malloc(sizeof *tmp);
fscanf(fp, "%s", tmp->mail);
tmp->next = head;
head = tmp;
这篇关于链表使用情况,以获得文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文