strtok again

This is a discussion on strtok again within the C Programming forums, part of the General Programming Boards category; ok I am trying to read in from a file using fgets the first part of the file looks like ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    strtok again

    ok I am trying to read in from a file using fgets
    the first part of the file looks like this


    diagram,Human Circulatory System
    :
    : blah blah blah
    : blah blah blah
    :
    vertex,1,brain

    I don't need to save the part that says blah blah blah

    I have put something in my code to skip over the lines that start with a colon but whenever I try to start reading again, there is a problem where I have indicated
    I assume it is because strtok can't use NULL as a starting point because those lines were skipped over.

    I am trying to read in the first line after the comments up to the comma. Once I have read that in I will proceed conditionally, based on what the string is.
    So after I have read in the graph type and title, the next thing to read will be vertex, and then proceed based on the vertex conditions


    Code:
    while (fgets(line,100,readfile) != NULL) 
    	{
    		
    		strcpy(Graph->type, strtok(line,","));
    		strcpy(Graph->title, strtok(NULL,"\n"));
    		if(line[0] != ':')//if line isn't a comment
    		{
    						strcpy(info, strtok(NULL,","));//problem is here
    			
    			if(strcmp(info, vertex) == 0)
    			{
    	
    				strcpy(temp, strtok(NULL,","));
    				vertex_ptr->inumber = atoi(temp);
    				strcpy(vertex_ptr->city, strtok(NULL,"\n"));
    			}
    			if(strcmp(info, edge) == 0)
    			{
    
    				strcpy(temp, strtok(NULL,","));
    				edge_ptr->iedge_number = atoi(temp);
    				strcpy(temp, strtok(NULL,","));
    				edge_ptr->ideparture = atoi(temp);
    				strcpy(temp, strtok(NULL,","));
    				edge_ptr->idestination = atoi(temp);
    				strcpy(temp, strtok(NULL," "));
    				edge_ptr->edge_weight = atof(temp);
    			}
    
    		}
    
    			
    			
    	}
    can anyone see how I can fix this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    strcpy(Graph->type, strtok(line,","));
    strcpy(Graph->title, strtok(NULL,"\n"));

    These appear inside your while loop, yet they only apply to the first line of the file

    So I would perhaps
    Code:
    fgets(line,100,readfile);
    strcpy(Graph->type, strtok(line,","));
    strcpy(Graph->title, strtok(NULL,"\n"));
    while (fgets(line,100,readfile) != NULL) {
    ...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: strtok again

    I'd also add:
    Code:
    while (fgets(line,100,readfile) != NULL) 
    	{
    	    if (line[0] != ':')
    	    {
    		strcpy(Graph->type, strtok(line,","));
    ...
    and just take care of the colon directly. Why go thru all that strtok() stuff on a comment line?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

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