[英]In C, how can i read data from a text file and insert them an linked list that organised in an ascending order according to the first names?
The text file "mh.txt" includes the following names, surnames, gender and birth date of 3 people separated by semicolons. 文本文件“ mh.txt”包含以下名称,姓氏,性别和3个以分号分隔的人的出生日期。
which look like this: 看起来像这样:
Tiffany;Evans Smith;F;22/01/1989; Tiffany; Evans Smith; F; 22/01/1989;
Alex;Williams;M;23/06/1988; 亚历克斯;威廉姆斯;男; 1988年6月23日;
Clay;Bristol;F;30/12/1989; 克莱;布里斯托尔;女; 1989年12月30日;
I want to store every person's details in a node so we'd have 3 nodes in total. 我想将每个人的详细信息存储在一个节点中,因此我们总共有3个节点。 All connected in a linked list.
所有连接都在一个链表中。 Then I want to print the linked list using a print function.
然后,我想使用打印功能来打印链接列表。
The problem is that my compiler displays the message "fix.exe has stopped working" every time I compile and run! 问题是我每次编译并运行时,我的编译器都会显示消息“ fix.exe已停止工作”! my c file is called fix.c
我的c文件称为fix.c
This is my attempt: 这是我的尝试:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
#define SIZE 100
struct Node{
char fname[SIZE];
char sname[SIZE];
char gender;
char Byear[SIZE];
struct Node *next;
};
struct ListRecord
{
struct Node *head;
struct Node *tail;
int size;
};
typedef struct ListRecord List;
List* CreateList(void);
void MakeEmptyList(List *);
void printFriends(List *);
List * initialiseFL(List *);
int main(){
List *myList;
int option;
int exit=FALSE;
char fname[SIZE];
myList = CreateList();
myList=initialiseFL(myList);//reads data from text file and adds nodes
to linked list
while(!exit){
fflush(stdin);
printf("C:>FriendBook friends.txt\n");
printf("Your FriendBook has been created.\n");
printf("(1) Insert a new friend\n");
printf("(2) Print your friends\n");
printf("(3) Search for your friend\n");
printf("(4) Block your friend\n");
printf("(5) Print your blocked friend\n");
printf("(6) Exit\n");
printf("Enter your option:");
printf("\n\n");
scanf("%d",&option);
fflush(stdin);
switch(option){
case 2:
printFriends(myList);
break;
default:
printf("Command not recognised!!\n");
break;
}
}
return 0;
}
List* CreateList(){
List *l;
l = (struct ListRecord *) malloc(sizeof(struct ListRecord));
if (l == NULL)
printf("Out of memory!\n");
MakeEmptyList(l);
return l;
}
void MakeEmptyList(List *l)
{
l->head = (struct Node *) malloc(sizeof(struct Node));
if (l->head == NULL)
printf("Out of memory!\n");
l->head->next = NULL;
l->tail = l->head;
l->size = 0;
}
List* initialiseFL(List *l){
FILE *IN;
IN =fopen("mh.txt", "r");
if (IN == NULL){
printf("Could not open text file!!\n");
}
while((fgetc(IN))!=EOF){
struct Node *p;
p=(struct Node*) malloc(sizeof(struct Node));
char temp;
temp=fgetc(IN);
int i=0;
//read first name a char at a time until ;
while(temp!=';'){
p->fname[i]=temp;
i++;
temp=fgetc(IN);
}
p->fname[i]='\0';
//read second name a char at a time until ;
temp=fgetc(IN);
i=0;
while(temp!=';'){
p->sname[i]=temp;
i++;
temp=fgetc(IN);
}
p->sname[i]='\0';
//read gender from text file
temp=fgetc(IN);
p->gender=temp;
//read birth year from text file
temp=fgetc(IN);
temp=fgetc(IN);
i=0;
while(temp!=';'){
p->Byear[i]=temp;
i++;
temp=fgetc(IN);
}
p->Byear[i]='\0';
//if list is empty, add node right after the dummy
if (l->size==0){
l->head->next=p;
l->tail=p;
l->size++;
}
//if the list contanins one node after the dummy
struct Node *first;
first=l->head->next;
if(l->size==1){
if(strcmp(p->fname[0],first->fname[0])>0 && first->next==NULL){
p->next=NULL;
first->next=p;
l->tail=p;
l->size++;
}
else{
p->next=first;
l->head->next=p;
l->size++;
first=l->head->next;//reset "first" to point to the 1st
node in the list
}
}
struct Node *second;
second=first->next;
//if it is to be added at the beginning
if(l->size>=2){
if(strcmp(p->fname[0],first->fname[0])<0){
p->next=first;
l->head->next=p;
l->size++;
first=l->head->next;//reset "first" to point to the 1st node
in the list
}
else{
while(!(strcmp(p->fname,first->fname)>0 && strcmp(p-
>fname,second->fname)<0 )){
first=first->next;
second=second->next;
}
if(second->next==NULL){
p->next=NULL;
first->next=p;
}
else{
p->next=second;
first->next=p;
}
first=l->head->next;//reset "first" to point to the 1st node
in the list
second=first->next;//reset "second" to point to the 2st node
in the list
}
}
}
fclose(IN);
return l;
}
void printFriends(List *l){
//make a temp head that points to the dummy
struct Node *temp;
temp=(struct Node*) malloc(sizeof(struct Node));
temp=l->head->next;
printf("Your friends are listed below.\n");
printf("Name\tSurname\tGender\tBirth Year\n");
while(temp!=NULL){
printf("%s\t%s\t%c\t%s\n",temp->fname,temp->sname,temp-
>gender,temp->Byear);
temp=temp->next;
}
}
consider one of the statement of your code 考虑您的代码的声明之一
if(strcmp(p->fname[0],first->fname[0])<0){
strcmp() excepts both arguments as strings , but p->fname[0]
will result in single character
, just modify as below. strcmp()都将两个参数都作为字符串 ,但
p->fname[0]
将导致single character
,只需进行如下修改。
if(strcmp(p->fname,first->fname)<0){
And there is bug in below code, you are updating first
& second
not p
thats why loop is running infinitely. 下面的代码中有错误,您在更新
first
和second
不是p
这就是循环无限运行的原因。
while(!(strcmp(p->fname,first->fname)>0 && strcmp(p->fname,second->fname)<0 )){
first=first->next;
second=second->next;
}
I hope it will helps. 我希望这会有所帮助。
You can use the code below for your requirement. 您可以根据需要使用以下代码。
What is char Byear[SIZE];
什么是
char Byear[SIZE];
this field? 这个领域? As per your data file, it should be a
date
field and as forth field in data file is a date. 根据您的数据文件,它应该是
date
字段,而数据文件中的第四个字段是日期。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Friend
{
char *name;
char *sur;
char *gen;
char *date;
struct Friend *next;
};
struct Friend * initializeFL(char *);
void printFriends(struct Friend *);
int main()
{
printf("\nsdasda\n");
struct Friend *head;
char fname[100];
printf("Enter the name of file .txt: \t");
gets(fname);
head=initializeFL(fname);
printFriends(head);
}
struct Friend * initializeFL(char *fname)
{
FILE* fpointer;
char ch;
fpointer = fopen(fname,"r");
if(fpointer == NULL)
{
do
{
printf("\nFile not found, please enter the name of file again: \t");
gets(fname);
fpointer = fopen(fname,"r");
}while(fpointer == NULL);
}
//FILE IS OPENED
struct Friend *head=NULL;
struct Friend *t, *p, *q;
char line[255];
char sent[2]=";";
while(!feof(fpointer) && fgets(line,sizeof line,fpointer))
{
char *name=strtok(line,sent);
char *sur=strtok(NULL,sent);
char *gen=strtok(NULL,sent);
char *date=strtok(NULL,sent);
if(head == NULL)
{
head=(struct Friend*)malloc(sizeof(struct Friend));
t = head;
t->name = (char *)malloc(strlen(name));
t->sur = (char *)malloc(strlen(sur));
t->gen = (char *)malloc(strlen(gen));
t->date = (char *)malloc(strlen(date));
strcpy(t->name,name);
strcpy(t->sur,sur);
strcpy(t->gen,gen);
strcpy(t->date,date);
t->next=NULL;
}
else
{
p=(struct Friend*)malloc(sizeof(struct Friend));
p->name = (char *)malloc(strlen(name));
p->sur = (char *)malloc(strlen(sur));
p->gen = (char *)malloc(strlen(gen));
p->date = (char *)malloc(strlen(date));
strcpy(p->name,name);
strcpy(p->sur,sur);
strcpy(p->gen,gen);
strcpy(p->date,date);
p->next=NULL;
q = t = head;
int i = strcmp(p->name,t->name);
printf("i = %d\n",i);
while(t!=NULL && strcmp(p->name,t->name)> 0)
{
q = t;
t=t->next;
}
if(t == head)
{
p->next=head;
head=p;
}
else
{
q->next = p;
p->next = t;
}
}
}
return head;
}
void printFriends(struct Friend *head)
{
while(head!=NULL)
{
printf("Name :%s\n",head->name);
printf("Surname :%s\n",head->sur);
printf("Name :%s\n",head->gen);
printf("Name :%s\n",head->date);
head=head->next;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.