944,161 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6021
  • C RSS
Mar 9th, 2005
0

Adding to linked list from external file

Expand Post »
Hi,

I am writing a program at the moment which requires me to scan in data from an external file, which to be added to a linked list. The external file is a list of pairs of cities and the distances between them - each piece of data is tab-delimited, and each pair of cities is on a new line. Each node within the linked list should contain the name of city one, the name of city two, and the distance between them. My code so far is shown below:
  1. typedef struct node_pair_info // declare structure for npi database
  2. {
  3. char city1[20];
  4. char city2[20];
  5. int distance;
  6. struct node_pair_info *next;
  7. } NPI;
  8.  
  9. NPI *add_nodes(NPI * item)
  10. {
  11. int i;
  12. FILE *cities;
  13.  
  14. cities = fopen("ukcities.txt", "r");
  15. if (cities == NULL)
  16. printf("Unable to open ukcities.txt\n");
  17.  
  18. else {
  19.  
  20. NPI *new_node;
  21. new_node = (NPI *) malloc(sizeof(NPI));
  22. new_node->next = NULL;
  23. fscanf(cities, "%s \t %s \t %d\n", new_node->city1,
  24. new_node->city2, new_node->distance);
  25. gotoxy(20, 25);
  26. fprintf(stdout, "%s %s %d\n", new_node->city1, new_node->city2,
  27. new_node->distance);
  28.  
  29. }
  30.  
  31. fclose(cities);
  32.  
  33. return 0;
  34. }
  35.  
  36. int main(void)
  37. {
  38. NPI *first = NULL, *current = NULL, *last = NULL;
  39. char response;
  40.  
  41. system("cls");
  42.  
  43. gotoxy(25, 18);
  44. printf("Enter 'a' to add all cars to list");
  45. gotoxy(40, 25);
  46. putch(' ');
  47. gotoxy(25, 25);
  48. printf("Enter command: ");
  49. response = getch();
  50. gotoxy(34, 40);
  51. putch(response);
  52.  
  53. switch (response) {
  54. case 'a':
  55. current = last = add_nodes(last);
  56. break;
  57. }
  58.  
  59. if (first == NULL)
  60. current = first = last;
  61.  
  62. return 0;
  63. }
The way which i intend for it to work at the moment is that upon the user entering 'a', all the data will be added to the linked list. The program compiles, but upon running, crashes. Im not certain that the code is correct! If anybody could lend me a hand i would be most grateful!

Thanks,

Charlie
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cghv is offline Offline
1 posts
since Mar 2005
Mar 9th, 2005
0

Re: Adding to linked list from external file

One thing I see right away:
		fscanf(cities, "%s \t %s \t %d\n", new_node->city1,
			   new_node->city2, &new_node->distance);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: readin flip-flop charecteristic table
Next Thread in C Forum Timeline: How to free up memory used for queues?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC