Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem in parsing a file

Problem in parsing a file

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++json
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ykutanoor
    wrote on last edited by
    #1

    I am parsing a file where each record is separeated by comma. I am getting a minor problem here. I am getting one extra empty field. Here is the code.Pls help CStdioFile myfile; CString strRecord; try { myfile.Open("C:\\YKUTANOOR\\VC++\\config.txt", CFile::modeRead ); myfile.SeekToBegin(); while(myfile.ReadString(strRecord)!=NULL) { CString resToken; CString msgPromt; int curPos= 0; int countFields =0; resToken= strRecord.Tokenize(",",curPos); CString temp = resToken; AfxMessageBox(resToken); while (resToken != "") { resToken= strRecord.Tokenize(",",curPos); } } }catch() { }

    J 1 Reply Last reply
    0
    • Y ykutanoor

      I am parsing a file where each record is separeated by comma. I am getting a minor problem here. I am getting one extra empty field. Here is the code.Pls help CStdioFile myfile; CString strRecord; try { myfile.Open("C:\\YKUTANOOR\\VC++\\config.txt", CFile::modeRead ); myfile.SeekToBegin(); while(myfile.ReadString(strRecord)!=NULL) { CString resToken; CString msgPromt; int curPos= 0; int countFields =0; resToken= strRecord.Tokenize(",",curPos); CString temp = resToken; AfxMessageBox(resToken); while (resToken != "") { resToken= strRecord.Tokenize(",",curPos); } } }catch() { }

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      Do an additional test CStdioFile myfile; CString strRecord; try { myfile.Open("C:\\YKUTANOOR\\VC++\\config.txt", CFile::modeRead ); myfile.SeekToBegin(); while(myfile.ReadString(strRecord)!=NULL) { CString resToken; CString msgPromt; int curPos= 0; int countFields =0; resToken= strRecord.Tokenize(",",curPos); if(!resToken.IsEmpty()) { CString temp = resToken; AfxMessageBox(resToken); while (resToken != "") { resToken= strRecord.Tokenize(",",curPos); } } }catch() { } } Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      Y 1 Reply Last reply
      0
      • J jmkhael

        Do an additional test CStdioFile myfile; CString strRecord; try { myfile.Open("C:\\YKUTANOOR\\VC++\\config.txt", CFile::modeRead ); myfile.SeekToBegin(); while(myfile.ReadString(strRecord)!=NULL) { CString resToken; CString msgPromt; int curPos= 0; int countFields =0; resToken= strRecord.Tokenize(",",curPos); if(!resToken.IsEmpty()) { CString temp = resToken; AfxMessageBox(resToken); while (resToken != "") { resToken= strRecord.Tokenize(",",curPos); } } }catch() { } } Papa while (TRUE) Papa.WillLove ( Bebe ) ;

        Y Offline
        Y Offline
        ykutanoor
        wrote on last edited by
        #3

        Thnx for the modified code. But if u observe that the last message box(after fields are shown) are coming empty) I need to stop this. If u try ... take a file LIKE this and use the above code. 1000,Y,1 2000,N,1 3000,N,1 4000,N,2

        J S 2 Replies Last reply
        0
        • Y ykutanoor

          Thnx for the modified code. But if u observe that the last message box(after fields are shown) are coming empty) I need to stop this. If u try ... take a file LIKE this and use the above code. 1000,Y,1 2000,N,1 3000,N,1 4000,N,2

          J Offline
          J Offline
          jmkhael
          wrote on last edited by
          #4

          This works: CStdioFile myfile; CString strRecord; myfile.Open("C:\\config.txt", CFile::modeRead ); myfile.SeekToBegin(); while(myfile.ReadString(strRecord)!=NULL) { CString resToken; CString msgPromt; int curPos= 0; int countFields =0; while((curPos= strRecord.Find(",")) != -1) { resToken = strRecord.Left (curPos); AfxMessageBox(resToken); strRecord = strRecord.Mid (curPos + 1); } AfxMessageBox(strRecord); Papa while (TRUE) Papa.WillLove ( Bebe ) ;

          1 Reply Last reply
          0
          • Y ykutanoor

            Thnx for the modified code. But if u observe that the last message box(after fields are shown) are coming empty) I need to stop this. If u try ... take a file LIKE this and use the above code. 1000,Y,1 2000,N,1 3000,N,1 4000,N,2

            S Offline
            S Offline
            Stlan
            wrote on last edited by
            #5

            Retrieve your strings inside the while loop and just before the call to the Tokenize function. int Position = 0; CString Token(strRecord.Tokenize(_T(","), Position)); while (!Token.IsEmpty()) { TRACE(_T("[%s]\n"), Token); Token = strRecord.Tokenize(_T(","), Position); } With your example file, then I get: [1000] [Y] [1] [2000] [N] [1] [3000] [N] [1] [4000] [N] [2]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups