strtok tokenizing on spaces as well as my delimiter

This is a discussion on strtok tokenizing on spaces as well as my delimiter within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am trying to read from a file that contains string records and copy the elements to a ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    strtok tokenizing on spaces as well as my delimiter

    Hi all,

    I am trying to read from a file that contains string records and copy the elements to a struct upon startup of the program. Each record is on one line and the elements of the record are separated by the delimeter ";". The problem is that some elements, like "name" have two parts (eg. john smith;labourer;555-5555).

    I am using strtok to separate the strings. Here is the basic code that performs this:

    Code:
                    while (!b_file.eof()) {
                    
                        b_file>>str;
                        char * pch;
                        pch = strtok (str,";");
                        int var = 0;
                        while (pch!=NULL)
                        {
                            if (var == 0){
                                strcpy(emp[numEmployees].name, pch);
                            }
                            if (var == 1){
                                strcpy(emp[numEmployees].department, pch);
                            }
                            if (var == 2){
                                strcpy(emp[numEmployees].job, pch);
                            }
                            if (var == 3){
                                strcpy(emp[numEmployees].phone, pch);
                            }
                            var++; 
                            pch = strtok (NULL, ";");
                        }
                    numEmployees++; 
                    }
    If I save the name as "John" instead of "John Smith" the code works fine. If the name has 2 parts then it creates a new record in the struct containing the remaining parts of the record.

    eg:

    john null null
    smith labourer 555-5555

    I was under the impression that strtok tokenized only on the delimeter that I specidied, but it seems to also tokenize on the space by default.

    Does anyone have any quick and clean solutions to this problem?

    Thanks,

    snowblind37

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Your problem is here:
    [code]
    b_file>>str;
    [code]
    This will read from the file untill it reaches a space
    you should try using getline(), there is nothing wrong with strtok()
    Last edited by ammar; 06-15-2004 at 12:02 AM.
    none...

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Thanks Metal Head,

    Nothing like a good, clean, one line fix. That did the trick!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokenizing an expression, strtok or other alternatives?
    By _Marcel_ in forum C Programming
    Replies: 3
    Last Post: 03-04-2009, 07:57 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  3. strtok with "" as delimiter?
    By fanoliv in forum C Programming
    Replies: 15
    Last Post: 06-19-2006, 12:44 PM
  4. scanf - strtok and space as delimiter?
    By Bill Cosby in forum C Programming
    Replies: 6
    Last Post: 09-20-2004, 06:45 PM
  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