| | |
Adding to linked list from external file
![]() |
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
Solved Threads: 0
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:
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
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:
C Syntax (Toggle Plain Text)
typedef struct node_pair_info // declare structure for npi database { char city1[20]; char city2[20]; int distance; struct node_pair_info *next; } NPI; NPI *add_nodes(NPI * item) { int i; FILE *cities; cities = fopen("ukcities.txt", "r"); if (cities == NULL) printf("Unable to open ukcities.txt\n"); else { NPI *new_node; new_node = (NPI *) malloc(sizeof(NPI)); new_node->next = NULL; fscanf(cities, "%s \t %s \t %d\n", new_node->city1, new_node->city2, new_node->distance); gotoxy(20, 25); fprintf(stdout, "%s %s %d\n", new_node->city1, new_node->city2, new_node->distance); } fclose(cities); return 0; } int main(void) { NPI *first = NULL, *current = NULL, *last = NULL; char response; system("cls"); gotoxy(25, 18); printf("Enter 'a' to add all cars to list"); gotoxy(40, 25); putch(' '); gotoxy(25, 25); printf("Enter command: "); response = getch(); gotoxy(34, 40); putch(response); switch (response) { case 'a': current = last = add_nodes(last); break; } if (first == NULL) current = first = last; return 0; }
Thanks,
Charlie
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
![]() |
Similar Threads
- Help me please with reading in linked list txt file (C++)
- Using Linked List to write to a file (C)
- linked list and text file (C++)
Other Threads in the C Forum
- Previous Thread: readin flip-flop charecteristic table
- Next Thread: How to free up memory used for queues?
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






