Split a String into Tokens

This is a discussion on Split a String into Tokens within the C++ Programming forums, part of the General Programming Boards category; I am trying to split a string into tokens. ("file1.txt") contains this row: 12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22 What I am trying to do ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Split a String into Tokens

    I am trying to split a string into tokens.
    ("file1.txt") contains this row: 12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22

    What I am trying to do now is to split this row up into tokens where the "," is the separator. The output that comes from this row will I put in ("file2.txt")

    My code that I have tried to build look like this. I suppose I am on the right track but I cant find the ´wrong´ in the code.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    int main()
    {
    char Rad[100];
    char * Tecken;
    //
    ofstream Test;
    Test.open ("file2.txt");
    ifstream myfile ("file1.txt");
    //
    myfile >> Rad;
    //
    printf ("Splitting string \"&#37;s\" into tokens:\n",Rad);
    Tecken = strtok (Tecken,",");
    while (Tecken != NULL)
    {
       printf ("%s\n",Tecken);
       Tecken = strtok (NULL, ",");
    
       Test << Tecken <<"\n";
    }
       Return 0;
    }
    Last edited by Coding; 12-17-2007 at 12:20 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    1: Indent code properly, please.
    2: myfile >> Rad is so unsafe. Try getline(myfile, Rad) instead (and make Rad std::string).
    Code:
    ifstream myfile ("file1.txt);
    Missing " at the end of file1.txt.

    Code:
    Tecken = strtok (Tecken,",");
    What do you suppose this does? Potentially a crash! Tecken isn't pointing to anything!
    Last edited by Elysia; 12-17-2007 at 11:53 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You could read in a line with getline, then put that string into a stringstream and read in each token with getline again, but using the stringstream and setting the delimiter to ',' instead of '\n'.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I have tried to do the changes mentioned about the delimiter, std::string and getline(myfile, Rad).
    But still I haven&#180;t figured it out if I am really doing right here.
    I beleive I might have to see how this code could be written. Perheps using my code above wich also telling what I want to do and do the changes in that...
    I beleive it is more simplier like that for me to understand. Then I can analyze what is happening in the code more easy.
    Last edited by Coding; 12-17-2007 at 12:19 PM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I trying to solve my problem in another way. I have made a code below that works for taking the first characters out before the "First delimiter", in this case ',' . The line is as below:
    12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22

    What I have done here with my code is to copy: 12/04/2007 to ("file2.txt").

    Now is my Question this: How can I now instead assign: 2111 to the: std::string tal, wich comes After the First delimiter (',') ?
    How could the code look like for this..
    Thank You...
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    
    { 
    	std::string tal;
        ofstream Test;
      Test.open ("file2.txt");
      ifstream myfile ("file1.txt");
      ifstream file0 ("file0.txt");
    
    getline(myfile, tal, ',');
      
    
      Test << tal <<"\n";
      return 0;
    }
    Last edited by Coding; 12-17-2007 at 01:39 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Have you looked into using MFC? If you have a non-Express version of Visual Studio, then MFC is possible (though it won't be portable).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >Tecken = strtok (Tecken,",");
    Change this to:
    Code:
    Tecken = strtok (Rad,",");

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    My version is: "Microsoft Visual C++ 2008 Express Edition". I don&#180;t know to much about MFC so I dont know the reason you mentioned it, sorry.. I know I have Somthing called "Windows Forms Application". I dont know if this is something simular to MFC ?

    I wonder still though how it is possible to solve the problem how to put "2111" in my last post...

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Don't know. Express doesn't have MFC; it needs to be Standard or Professional, I believe. Non-free, of course, much to our disappointment.
    MFC has so much better classes than the STL, which is why I mention it.
    Last edited by Elysia; 12-17-2007 at 02:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    ok I understand. This is the first edition I use, so I dont understand all the differences between them yet.
    I will try to work this out first and perheps I will change later to the Proffesional edition.. Thanks for your help !

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Remember that it isn't free. I don't know much about the differences either, other than it comes with MFC.
    MFC is great for native Windows programming; it also has lots of utility classes that's better than STL for professional developers; one I assume you are aiming to become.
    Anyway, I'm sure someone else knows a lot more about C++ I/O than I do and can help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes I aiming to be a professional developer Actually as you are describing I am up to develop a special program in the end wich will be a "Windows application".
    The things I am doing now is just basics stuff to understand the language.

    Do you think it is to early for me to instead of using the edition I use now to continue with the C++ professional and focus more on building Windows applications with buttons, menus etc..

    Perheps a difficult question to answer, I dont know .
    Someone told me before it is better to begin with simple consoleprograms to first understand the language better before starting to do "Windows applications programs/forms".

    Thanks Elysia

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Coding View Post
    I trying to solve my problem in another way. I have made a code below that works for taking the first characters out before the "First delimiter", in this case ',' . The line is as below:
    12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22

    What I have done here with my code is to copy: 12/04/2007 to ("file2.txt").

    Now is my Question this: How can I now instead assign: 2111 to the: std::string tal, wich comes After the First delimiter (',') ?
    Keep reading? The rest of the line is still there (you only removed up to (and including) the comma). In other words, if you put another
    Code:
      getline(myfile, tal, ',');
      Test << tal <<"\n";
    at the end, you should then see 2111 in your file2.txt.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Yes, you certainly need more experience with the basics; but the edition does not define what you can in sense of the language. The compiler is the same in all. The only thing that might be different is that you have MFC at your hands, which makes things easier.
    With MFC, for example, you don't need to mess around with C-style functions such as strtok and things just to work with strings. MFC has a fully functional string class itself called CString that's far more useful than std::string.
    I do myself use MFC and I'm very used to it which is also why I know so little of STL I/O and strings (and to know the truth, I frown at STL I/O and strings). That is why I recommend MFC if you want to program for Windows.

    You can use whatever version and still continue with the basics...
    Windows programming is the next step, after you learn the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I dislike strtok from the bottom of my heart. It is simply too easy to make errors with it.
    If I was doing this, and if I had no performance limitations, I would coock up my own 'explode' function (as explode in php).
    It would eat a string and delimiter, and return a vector<string> containing the pieces.

    Simple way to do it would be:
    read the whole thing in a string
    place it in your own explode function

    In function:

    use string.rfind() to locate last ','.
    use string.substr and vector.push_back to store the last piece in vector
    use string.erase to remove the last piece from string.

    repeat these untill every ',' is handled

    store the first piece in vector.
    [optional]
    change the order of pieces in vector.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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