reading file and strtok

This is a discussion on reading file and strtok within the C Programming forums, part of the General Programming Boards category; I have a text file that has about 50 lines of text like this: Nancy Bush;2607 Avenue;Atlanta I am sopossed ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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 01:13 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 02:11 AM.
    Hope is the first step on the road to disappointment.

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

    because the first delimiter is also ; not ,
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 02:12 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    also some typo
    sscanf( buf, "%[^;]%*c%[^;]%*c%[^\n]%*c",
    red - mandatory moved
    blue - optional, can be removed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    so I was quick
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 02: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, 11:54 AM

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