[help] - storing data from txt in linked list

This is a discussion on [help] - storing data from txt in linked list within the C Programming forums, part of the General Programming Boards category; Hi, I am having a little trouble with my program, what I am trying to achieve is storing the data(each ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    [help] - storing data from txt in linked list

    Hi,

    I am having a little trouble with my program, what I am trying to achieve is storing the data(each line) of the BATTING.txt in to a different element of a linked list.

    I am getting a segmentation error when trying to run the below program, I am new to c programming and am unsure of how to find the solution to it.

    Sorry for such a long post.

    *** this is my code ***
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct player
    {
    	char *line;
    	struct player *next;
    } player;
    
    typedef struct player playerlist;
    
    // create head of the list of players 
    playerlist *head = NULL;
    
    void addPlayer(char *info);
    
    int main(int argc, char *argv[])
    {
    	// create file
    	FILE *battingFile;
    
    	// open file
    	battingFile = fopen("BATTING.txt","r");
    
    	// error if cannot open
    	if (battingFile == NULL) 
    	{
    		printf("Can't open input file!\n");
    	}
    
    	char line[300];
    	
    	// get line
    	while (fgets(line, 300 ,battingFile))
    	{
    		while(line[0] != '#')
    		{
    			addPlayer(line);
    			break;
    		}
    	}
    	fclose(battingFile);
    	return 1;
    }
    
    void addPlayer(char *info)
    {
    	playerlist *current, *temp;
    	current = head;
    	temp = NULL;
    	temp->line = info;
    	temp->next = NULL;
    	
    	while(current->next != NULL)
    	{
    		current = current->next;
    	}	
    	current->next = temp;
    }
    *** BATTING.txt file ***
    Code:
    # Do not modify this file
    #
    # Comment lines start with a #
    #
    # These are the batting stats for the 2000/2001 test series
    # Australia vs West Indies
    #
    #
    # The names of the fields are
    # Name, Country, Innings, Total Runs, Times Not Out, Highest Score, Highest Score was not out
    #
    #NAME		COUNTRY	INNINGS	RUNS	N.OUT	H.SCORE	HS is NO
    #
    Adams		WI	10	151	2	49	FALSE
    Bichel		AUS	2	11	0	8	FALSE
    Black		WI	6	6	2	3	TRUE
    Campbell WI	10	187	0	79	FALSE
    Chanderpaul	WI	2	80	1	62	TRUE
    Dillon		WI	8	73	0	27	FALSE
    Ganga		WI	8	107	0	32	FALSE
    Gilchrist             AUS	6	241	1	50	FALSE
    Gillespie            AUS	4	48	0	23	FALSE
    Hayden		AUS	8	236	0	69	FALSE
    Hinds		WI	8	247	0	70	FALSE
    Jacobs		WI	10	288	1	96	TRUE
    Langer		AUS	8	203	0	80	FALSE
    Lara		WI	10	321	0	182	FALSE
    Lee		AUS	2	103	2	62	TRUE
    MacGill		AUS	4	44	1	19	FALSE
    Martyn		AUS	2	80	2	46	TRUE
    McGrath		AUS	4	25	0	13	FALSE
    McLean		WI	10	64	0	17	FALSE
    Miller		AUS	4	78	1	37	TRUE
    Nagamootoo	WI	2	80	0	68	FALSE
    Ponting		AUS	8	242	1	92	FALSE
    Samuels		WI	6	172	1	60	TRUE
    Sarwan		WI	6	54	0	51	FALSE
    Slater		AUS	8	373	1	96	FALSE
    Stuart		WI	4	21	1	12	TRUE
    Walsh		WI	10	19	2	9	FALSE
    MWaugh		AUS	6	349	1	121	TRUE
    SWaugh		AUS	8	339	1	119	FALSE

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Were you planning to ever create any player objects?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    I thought that was what my addPlayer() was doing creating a player from information and adding it to the end of the list.

    Or that is what it is supposed to do.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't actually allocating any nodes. You're also just assigning a pointer to the string passed, and then overwriting it on your next read from the file. You need to be duping the string you pass if you actually want to keep that data.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21