Thread: reading file and strtok

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    reading file and strtok

    I have a text file that has about 50 lines of text like this:
    Nancy Bush;2607 Avenue;Atlanta

    I am sopossed to put it into a hashtable, but right now im just reading in the text.

    My snipped of code can read the whole file in, giving me all the names, but how can i read in the street and city too? Do i just do another strtok right after the first one? (just tried it and doesnt work)

    Code:
    FILE *filelist;
          filelist=fopen("fileit.txt", "r");
          char line[max_string_length];
          char *name,*street,*city;
    
          while(fgets(line, sizeof(line), filelist)!=NULL){
             name=strtok(line,";");
             street=strtok(line,";");
             printf("%s\n",name);
             printf("%s\n",street);
          }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Have you tried
    strtok(NULL,";")
    for the 2nd and 3rd call?
    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main()
    {
    	char line[] = "Nancy Bush;2607 Avenue;Atlanta"
    		;
    	char *name,*street,*city;
    	name=strtok(line,";");
    	street=strtok(NULL,";");
    	city=strtok(NULL,";");
    	
    	printf("%s\n",name);
    	printf("%s\n",street);
    	printf("%s\n",city);
    	return 0;
    }
    Output:
    Code:
    Nancy Bush
    2607 Avenue
    Atlanta
    Last edited by vart; 12-02-2006 at 02:13 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void strtokisforsuckers( void )
    {
        FILE *fp = fopen( "yourfile", "yourmode" );
        if( fp )
        {
            char buf[ BUFSIZ ];
            struct foo bar;
    
            while( fgets( buf, BUFSIZ, fp ) != NULL )
            {
                if( sscanf( buf, "%[^;]%*c%[^;]%*c%[^\n]%*c", bar.name, bar.addy, bar.city ) == 3 )
                    printf("%s\n%s\n%s\n", bar.name, bar.addy, bar.city );
            }
    
            fclose( fp );
        }
    }
    *edits
    *more edits!

    Quzah.
    Last edited by quzah; 12-02-2006 at 03:11 AM.
    Hope is the first step on the road to disappointment.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    sscanf( "%[^,]%[^;]%[^\n]",
    should be
    sscanf( "%[^;]%[^;]%[^\n]",

    because the first delimiter is also ; not ,
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, and I left out the first argument. I thought he was delimiting with a , the first time and a ; the second. It also needs a few minor corrections, to get rid of the ;, which I've added. This, kiddies, is why you should use your preview button and not just type as you go, like I do.


    Quzah.
    Last edited by quzah; 12-02-2006 at 03:12 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    also some typo
    sscanf( buf, "%[^;]%*c%[^;]%*c%[^\n]%*c",
    red - mandatory moved
    blue - optional, can be removed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah you caught me in mid-edit. I tend to just hit edit over and over and use "save" instead of preview.


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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    so I was quick
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in a two line file
    By deadpickle in forum C Programming
    Replies: 1
    Last Post: 05-08-2009, 05:35 PM
  2. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM
  3. reading, editing and creating a new file
    By s_jsstevens in forum C Programming
    Replies: 3
    Last Post: 04-27-2007, 02:40 PM
  4. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  5. Reading in File Help
    By swayp in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2005, 12:54 PM