Adding to linked list from external file

Reply

Join Date: Mar 2005
Posts: 1
Reputation: cghv is an unknown quantity at this point 
Solved Threads: 0
cghv cghv is offline Offline
Newbie Poster

Adding to linked list from external file

 
0
  #1
Mar 9th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Adding to linked list from external file

 
0
  #2
Mar 9th, 2005
One thing I see right away:
		fscanf(cities, "%s \t %s \t %d\n", new_node->city1,
			   new_node->city2, &new_node->distance);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC