Thread: Split a String into Tokens

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    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 01:20 PM.

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