Hello everyone! I've been trying to write my homework assignment (a list of countries, their codes and their capitals) and I've done most of it but I'm stuck at this: I have to open a file, read it and if there are data, add them to the list. So far I've created an element of the structure, queue list, printed the list on the screen and freed the memory. I thought that for reading the file and adding the data I could first open the file (of course) with fopen and after that use a for loop (i=0;i=!EOF;i++) to scan the whole file and fscanf(fp,"%s",result->country),etc in the loop to add the data to the structure of the element and finally insert that element to the queue list. However, when I try to do these operations, I only get to writing the name of the file and the program crashes. Can you help me figure out what I'm doing wrong?
Here's my code (I've copied it without the function for reading though)
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct List
{
char country[20];
char code[5];
char capital[20];
struct List*next;
};
typedef struct List LIST;
LIST *head;
LIST *current;
LIST *new_item();
LIST *create_queue_list(int size);
void find_item_cap(LIST *head,char *criteriaCap);
void find_item_country(LIST *head,char *criteriaCountry);
void print(LIST *head);
void free_list(LIST *head);
int main()
{
LIST *head=NULL;
int size;
char criteriaCap[20];
char criteriaCountry[20];
printf("How many countries? ");
scanf("%d",&size);
head=create_queue_list(size);
printf("\nList is: \n");
print(head);
printf("\nEnter the capital: ");
fflush(stdin);
gets(criteriaCap);
find_item_cap(head,criteriaCap);
printf("\nEnter the country: ");
fflush(stdin);
gets(criteriaCountry);
find_item_country(head,criteriaCountry);
free_list(head);
return 0;
}
LIST *new_item()
{
LIST *result=NULL;
char country[20];
char code[5];
char capital[20];
result=(LIST*)malloc(sizeof(LIST));
if(result==NULL)
{
printf("\nNot enough memory.");
}
else
{
printf("\nEnter the country: ");
fflush(stdin);
gets(country);
printf("Enter the country's code: ");
fflush(stdin);
gets(code);
printf("Enter the country's capital: ");
fflush(stdin);
gets(capital);
strcpy(result->country,country);
strcpy(result->code,code);
strcpy(result->capital,capital);
result->next=NULL;
}
return result;
}
LIST *create_queue_list(int size)
{
LIST *res_root=NULL;
LIST *end_item=NULL;
int i;
for(i=0;i<size;i++)
{
if(res_root==NULL)
{
res_root=new_item();
end_item=res_root;
}
else
{
end_item->next=new_item();
end_item=end_item->next;
}
}
return res_root;
}
void find_item_cap(LIST *head,char *criteriaCap)
{
LIST *curr_item=head;
while(curr_item!=NULL)
{
if(strcmp(curr_item->capital,criteriaCap)==0)
{
printf("%s is %s's capital.\n",criteriaCap,curr_item->country);
}
curr_item=curr_item->next;
}
}
void find_item_country(LIST *head,char *criteriaCountry)
{
LIST *curr_item=head;
while(curr_item!=NULL)
{
if(strcmp(curr_item->country,criteriaCountry)==0)
{
printf("%s's capital is %s.\n",criteriaCountry,curr_item->capital);
}
curr_item=curr_item->next;
}
}
void print(LIST *head)
{
LIST *curr_item=head;
while(curr_item!=NULL)
{
printf("%s %s %s\n",curr_item->code,curr_item->country,curr_item->capital);
curr_item=curr_item->next;
}
}
void free_list(LIST *head)
{
LIST *curr_item=head;
while(head!=NULL)
{
head=head->next;
free(curr_item);
curr_item=head;
}
}