reading a text file and "tokenizing" it into different types(int, char, double)

This is a discussion on reading a text file and "tokenizing" it into different types(int, char, double) within the C Programming forums, part of the General Programming Boards category; I am thinking of using fscans(??) to get a line at a time but how do i "tokenize" that string ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    reading a text file and "tokenizing" it into different types(int, char, double)

    I am thinking of using

    fscans(??)

    to get a line at a time but how do i "tokenize" that string ??


    thanks for your time and understanding,
    bill

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    i got fgets to work and it displays the .dat file perfectly but,

    i need to have lines that begin with // to be skipped.

    i also need to tokenize the buffer[] (is strtok() as bad as people on here say ??)



    any assistance is greatly appreciated.


    thanks,
    bill




    #include <stdio.h>

    FILE *fp1;


    void Load_events()
    {

    //char logon[20] ;
    //int term;
    //double time;
    //int user;
    char buffer[1000];


    printf("before logon.dat opened\n");

    fp1 = fopen("LOGON.DAT","r");

    if (fp1 == NULL) // Invalid file
    printf("Invalid file\n");

    while(fgets (buffer,1000, fp1) != NULL)
    {
    printf ("%s", buffer);



    }
    fclose(fp1);
    printf("\nafter logon.dat closed\n");



    }


    main(int argc, char *argv[])
    {
    Load_events();
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    fgets() works and the data prints out exactly like it is in the .dat file.
    Now, i need to parse the buffered string.
    i tried using some code i got from a search on this board,
    (seps[], strtok(), all // ed below, but the first token from the buffered list that i get is garbage and i get compile errors on the code below that.

    any assistance is greatly appreciated!!

    thanks,
    bill

    Code:
    #include <stdio.h>
    #include <string.h>
    
    FILE *fp1;
    
    void Load_events() 
    {
    
    // char logon[20]; 
    //char  *logptr ;
    //double time;
    //int user;
    // int term;
    char buffer[1000] ; 
    //char seps[] = " \n,( )";
    
    
    // logptr = logon;
    printf("before logon.dat opened\n");
    
    fp1 = fopen("LOGON.DAT","r"); // for "filename", insert a file or var if
    if (fp1 == NULL)   // Invalid file
        printf("Invalid file\n");
    
    while(fgets (buffer,1000, fp1) != NULL)
      {
         if ( strncmp( buffer, "//", 2 ) == 0 ) continue;  // skip comment
         else
    	 {
                   
                  // logptr = strtok( buffer, seps ); /* Find first token*/
                  // term  = strtok( buffer, seps ); /* Find first token*/
                  // user   = strtok( buffer, seps ); /* Find first tok
     //  time   = strtok( buffer, seps ); /* Find first token*/
    // printf ("logon = %s\nterm  = %d\nuser   = %d\ntime   = %f\n", logon,term,user,time);
    
    	 }// end of else
      }
    
    fclose(fp1);
    printf("\nafter logon.dat closed\n");
    
    
    		
    }
    
    main(int argc, char *argv[])   
    {    
    		Load_events();
    
    }
    Last edited by billh; 09-04-2002 at 07:47 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    this is what logon.dat looks like


    Code:
    //  LOGON.DAT
    //
    //           terminal no.           user id                   time (in seconds)
    
    LOGON		7			3			21
    LOGON		3			1			4
    LOGON		2			4			212
    LOGON		4			2			5

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf is your friend.

    sscanf( buf, "%s%d%d%d" ... )

    strtok is horrible, IMO.

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

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >strtok is horrible, IMO.
    Horrible for the unwary maybe, but the only real problems strtok has is that it modifies the original argument and is not re-entrant. Being standard and relatively easy to use usually out-weighs those problems. What are your arguments against it?

    -Prelude
    My best code is written with the delete key.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > What are your arguments against it?

    The fact that it modifies the string passed it, and it ugly as sin. I mean, really, couldn't they have come up with something better than "all additionall calls take NULL as the argument"?

    That's just ugly code. Yeah, I know that's how it works, but looking at the code and trying to figure out what it does just by looking at the function itself:

    ptr1 = myfunction( string_to_modify );
    ptr2 = myfunction( NULL );
    ptr3 = myfunction( NULL );

    Just looking at those three lines, you're left scracthing your head going "WTF?".

    It's ugly code. That's my main complaint.

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

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It's ugly code. That's my main complaint.
    I agree with you on that, but you also have to consider that since it is relatively well-known there isn't generally a problem with not understanding the code, and even if it is ugly, the usefulness and standardization of the function make up for this IMHO.

    If you had the chance, how would you redesign strtok? Here is an old(reworked to remove icky things like void main) implementation I wrote when I was in school...be gentle :
    Code:
    #include <stdio.h>
    #include <string.h>
    
    static char *make_tok ( char *dst, char *src, char *delim )
    {
      char *from = src;
      char *to   = dst;
    
      while( *from != '\0' ){
        if ( strchr ( delim, *from ) != NULL )
          break;
        *to++ = *from++;
      }
      *to = '\0';
    
      return ( *dst != '\0' ) ? ++from : NULL;
    }
    
    int main ( void )
    {
      char buf[100];
      char *str = "my:string:tokenizing:function";
      char *remainder = str;
    
      while ( ( remainder = make_tok ( buf, remainder, ":" ) ) != NULL)
        printf ( "%s\n", buf );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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