Thread: Split a String into Tokens

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Great help Elysia... I will try to buy this version. I assume it is called: C++ Proffesional.
    It really sounds what I will need after I have learned the basics etc... But still also for the basics why it sounds perfect...

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A better name would be Split. I don't know why they chose such a stupid name for such a function.
    But sure, you could make such a function. Good training and it's very easy.

    I will try to buy this version. I assume it is called: C++ Proffesional.
    I... don't know about that.
    Standard costs $280 and Professional costs $730, at least on the prices I could find on Newegg.
    Haha... Very expensive.
    Ehhh, well... Standard could be had, I guess. It's just the price for a commercial professional product.
    Last edited by Elysia; 12-17-2007 at 03:38 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.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    ok, thanks I will search for these and see when I will get it

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see need for MFC here, nor is there any need to buy the Standard or Professional version of VC++. The solution is relatively simple with a stringstream and getline, and the learning curve for those features is probably smaller than for MFC.

    As for your attempt, Coding, you are missing the stringstream part. You have to read a line into a string with getline from the file. Then put that string into a stringstream and read in each value with ',' from there.

  5. #20
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I have succeded with a code that takes the First and Second value out of: 12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22
    wich is: 12/04/2007 and 2111

    The second step after this is to tell for the code below: If Time == 2111 (wich it is)
    it will write the Time to the file. I have tried to do an approch but it doesn´t work.
    Perheps I have missed something here.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    
    { 
    	std::string Date;
    	std::string Time;
        ofstream Test;
      Test.open ("file2.txt");
      ifstream myfile ("file1.txt");
      ifstream file0 ("file0.txt");
    
    getline(myfile, Date, ',');// myfile >> tal;
    getline(myfile, Time, ',');// myfile >> tal;
    
    If (Time == 2111)                                     // This is what I am trying to do here: If Time is 2111 wich it is in this case.
    {  
    
      Test << Time <<"\n";    }
      return 0;
    }

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    I don't see need for MFC here, nor is there any need to buy the Standard or Professional version of VC++. The solution is relatively simple with a stringstream and getline, and the learning curve for those features is probably smaller than for MFC.
    No, and of course not. I don't recommend buying Standard anytime soon, or at all. But MFC would be of help. The learning curve for this using MFC is much smaller I believe, though. But it seems it's not an option since the OP does not have Standard+.
    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. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Time is a string variable. If you want to compare it to something you have to compare it to "2111" (with the quotes) because that specifies that it is a string.

    BTW, another option is if you know the exact format of your code (date as string,time as int,some other data as double,some other data as double,some other data as double,some other data as double,some other data as int,some other data as double), then you can read in with operator>> and then just read in the delimiters and ignore them.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Getline reads a full line of text - that is why it's called "getline" - so you get that entire line in one, then you get "nothing" in the next stage.

    You also can't compre a string with a number. You probably want to split the string, and convert the parts into the right form of number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Getline reads a full line of text
    getline reads up to the delimiter specified, so if you specify ',' as the delimiter, it will stop at the first comma. It is called getline because the default delimiter is a newline.

  10. #25
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I tried to compile with the "2111" before but this isn&#180;t working.
    As you say I want the Date to be a string ("") and the Time to be an Int.

    So I have changed to this:
    int Time = 0; //std::string Time;
    and still using:
    If (Time == 2111)

    But still for my code above I have no compile success..

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you read in with getline, it reads in a string, so just changing Time to an int won't solve the problem. You have to read in as a string, then convert to an int.

    This is what made me suggest the alternate approach of not using getline for the time.

    You can use getline for the date, because it is a string and it is difficult to tell where the end of the date is unless you read it character by character.

    Then, you can use operator>> to read in the Time. This will stop at the comma because a comma is not part of a number. It will allow you to make Time an int because operator>> works on ints.

    Next, you'll have to remember to read in the ',' character. The getline function will read in and remove the comma it finds, but operator>> will not. So just make a dummy char variable and read the comma into that variable with operator>>. Then you will be able to continue and read in the rest of the data.

  12. #27
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I follow your description but I am not sure practically how to do. I have experimented with some chars and >> but really dont understand the logic.

    I have succeded and understand the logic of how to put Date as string and Time as Int like this:

    getline(myfile, Date, ',');
    myfile >> Time;


    where the ',' is to follow for the Time(int) under this. But from here I dont know how to write the code really for the next value(35.23) in: 12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22

    I might need some codeassistance perheps...
    Last edited by Coding; 12-17-2007 at 05:01 PM.

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're doing fine. After the code you posted above, the input buffer looks like this:
    Code:
    ,35.23,35.23,35.20,35.22,15600,35.22
    Now just add another line using operator>> that reads into a char variable. This will read in the ',' character. Then the input buffer will look like:
    Code:
    35.23,35.23,35.20,35.22,15600,35.22
    Then you can use operator>> to read into a double variable to store the 35.23. And so on...

  14. #29
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I beleive this was my problem I am struggling with. I managed to do:
    getline(myfile, Date, ',');
    myfile >> Time;
    myfile >> Comma; //Comma is: char Comma[50];

    The output for: Comma in this case is: ,35.23,35.23,35.20,35.22,15600,35.22

    If I understand you right here I &#180;just&#180; want to put the ',' in the char Comma using
    >> and then a New line under this that reads in the: 35.23 using a double varibale.

    I am not sure how I will write this. If you have the time I would appreciate how you would continue these 2 lines under:
    getline(myfile, Date, ',');
    myfile >> Time;

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> //Comma is: char Comma[50];
    There's your problem.

    A comma is a single character, but you declared the Comma variable as an array of 50 characters. It should just be a char, not a char array.

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, 02:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM