Thread: Split a String into Tokens

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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;
    }

  2. #2
    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.

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