Click here to Skip to main content
11,802,205 members (53,012 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: MFC VC++
Please help me with string tokanize. my input file is text file such as

1,xyz,1 HP-1 Sigma,1.0,Y,Single,,
2,dfg,32 KES LW0090 ,1.0,Y,Single,,
with this code I am getting assertion iStart>=0 and only one string.(except 1)
xyz,1 HP-1 Sigma,1.0,Y,Single
this string

my code :
if(File.Open(pathName, CFile::modeRead)) // Open file to be read 
    while(File.ReadString(strLine)) // Read file 
        int Position = 0;
        CString Token;
        Token = strLine.Tokenize(",", Position);
        while(Token!=" ")
         int total = (int)m_List2.GetItemCount();
         for ( int ii=0; ii< total; ii++ )//for row
            for(int j=0; j<=6;j++) //for columns
                CString Token=strLine.Tokenize(",",Position);
    //AfxMessageBox(strMsg, MB_ICONINFORMATION); 
    // Display resulting contents of strMsg in a message box 
Posted 7-Feb-13 4:25am
Edited 7-Feb-13 5:43am
Chris Reynolds (BNY) at 7-Feb-13 11:32am
I'm not quite sure what you're trying to do as you're looping through each line in the file, then looping through rows in a list so you're processing the line too often as you'll have exhausted the line by the time you've completed the first iteration round the ii loop. Other than that I'd check for Position != -1 rather than Token !=" " in your while loop
peoria123 at 7-Feb-13 11:44am
I want to open text file into Listcontrol. listcontrol has 6 columns,
1,xyz,1 HP-1 Sigma,1.0,Y,Single,,
(comma separeted columns)
Thank you.
Sergey Alexandrovich Kryukov at 7-Feb-13 12:01pm
MFC..?! Why not using the standard things? std::string and everything else of std?
H.Brydon at 7-Feb-13 13:05pm
Nothing wrong with CString. It also has tokenizing features that std::string doesn't have.

I would do it with CString.
Sergey Alexandrovich Kryukov at 7-Feb-13 13:41pm
Maybe, but it's still a proprietary thing. Why creating a vendor lock-in?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I disagree that CString/MFC is a bad idea (but agree that it may be considered "proprietary" Microsoft).

The CString class has a tokenizer built into it, described here[^]. Never mind the "CStringT" stuff ... it applies to CString.

Joseph Newcomer has written an excellent article about CString which can be found here[^], with the CP version here[^] (no special info on tokenizing in these articles).
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question. Using proprietary and outdated MFC is a really bad idea, especially for such universal chores.
You will get a better idea, for example, from here:[^].


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 KrunalRohit 341
2 F-ES Sitecore 295
3 Afzaal Ahmad Zeeshan 230
4 CHill60 195
0 OriginalGriff 2,715
1 Maciej Los 1,900
2 KrunalRohit 1,797
3 CPallini 1,550
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100