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. Parsing a CSV file - problem

Parsing a CSV file - problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdebuggingjsontutorial
4 Posts 2 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.
  • L Offline
    L Offline
    lbc
    wrote on last edited by
    #1

    Hello everybody, i am trying to parse a simple CSV file (see a little file sample below) using the functions found in the book 'PROFESSIONAL MFC with VC++6' by M. Blaszczak, Wrox. I am working with VC7. The functions work fine but unfortunately i can get only the first, say, N-1 values parsed instead of all the N wished ones. for example in the sample below i get parsed only: label0 .. label5 as labels row (missing label6) field0,f1,f2,f3,f4,f5 as first row of values (missing field f6) 0,10,20,30,40,50 as second row of values (missing 60) and so on... I mean only six values (with reference to the csv file sample), not all the seven values any help/tip will be gratly appreciated! Thanks in advance class CMyDoc : public CDocument { public: int CountChars(CString& ref, TCHAR ch = ',') const; CString GetField(CString& ref, int nIndex, TCHAR ch = ',') const; ... }; ... int CMyDoc::CountChars(CString& ref, TCHAR ch) const { LPCTSTR pstrBuffer = ref.LockBuffer(); int nCount = 0; while (*pstrBuffer != _T('\0')) { if (*pstrBuffer == ch) nCount++; pstrBuffer++; } ref.UnlockBuffer(); return nCount; } CString CMyDoc::GetField(CString& ref, int nIndex, TCHAR ch) const { CString strReturn; LPCTSTR pstrStart = ref.LockBuffer(); LPCTSTR pstrBuffer = pstrStart; int nCurrent = 0; int nStart = 0; int nEnd = 0; int nOldStart = 0; while (nCurrent <= nIndex && *pstrBuffer != _T('\0')) { if (*pstrBuffer == ch) { nOldStart = nStart; nStart = nEnd+1; nCurrent++; } nEnd++; pstrBuffer++; } ref.UnlockBuffer(); if (*pstrBuffer == _T('\0')) { TRACE("Warning: Couldn't find it.\n"); return strReturn; } return ref.Mid(nOldStart, nEnd-nOldStart-1); } ... // CALLING STUFF ... int nColumns = pDoc->CountChars(pDoc->m_strColumns); TRACE("N. col= %d, header titles= %s\n", nColumns, pDoc->m_strColumns); int nIndex; CString strCurrent; for (nIndex = 0; nIndex < nColumns; nIndex++) { ... print values ... } SAMPLE FILE.CSV label0,label1,label2,label3,label4,label5,label6 <-- labels row field0,f1,f2,f3,f4,f5,f6 <-- first row of values 0,10,20,30,40,50,60 <-- second row of values ... best regards

    N 2 Replies Last reply
    0
    • L lbc

      Hello everybody, i am trying to parse a simple CSV file (see a little file sample below) using the functions found in the book 'PROFESSIONAL MFC with VC++6' by M. Blaszczak, Wrox. I am working with VC7. The functions work fine but unfortunately i can get only the first, say, N-1 values parsed instead of all the N wished ones. for example in the sample below i get parsed only: label0 .. label5 as labels row (missing label6) field0,f1,f2,f3,f4,f5 as first row of values (missing field f6) 0,10,20,30,40,50 as second row of values (missing 60) and so on... I mean only six values (with reference to the csv file sample), not all the seven values any help/tip will be gratly appreciated! Thanks in advance class CMyDoc : public CDocument { public: int CountChars(CString& ref, TCHAR ch = ',') const; CString GetField(CString& ref, int nIndex, TCHAR ch = ',') const; ... }; ... int CMyDoc::CountChars(CString& ref, TCHAR ch) const { LPCTSTR pstrBuffer = ref.LockBuffer(); int nCount = 0; while (*pstrBuffer != _T('\0')) { if (*pstrBuffer == ch) nCount++; pstrBuffer++; } ref.UnlockBuffer(); return nCount; } CString CMyDoc::GetField(CString& ref, int nIndex, TCHAR ch) const { CString strReturn; LPCTSTR pstrStart = ref.LockBuffer(); LPCTSTR pstrBuffer = pstrStart; int nCurrent = 0; int nStart = 0; int nEnd = 0; int nOldStart = 0; while (nCurrent <= nIndex && *pstrBuffer != _T('\0')) { if (*pstrBuffer == ch) { nOldStart = nStart; nStart = nEnd+1; nCurrent++; } nEnd++; pstrBuffer++; } ref.UnlockBuffer(); if (*pstrBuffer == _T('\0')) { TRACE("Warning: Couldn't find it.\n"); return strReturn; } return ref.Mid(nOldStart, nEnd-nOldStart-1); } ... // CALLING STUFF ... int nColumns = pDoc->CountChars(pDoc->m_strColumns); TRACE("N. col= %d, header titles= %s\n", nColumns, pDoc->m_strColumns); int nIndex; CString strCurrent; for (nIndex = 0; nIndex < nColumns; nIndex++) { ... print values ... } SAMPLE FILE.CSV label0,label1,label2,label3,label4,label5,label6 <-- labels row field0,f1,f2,f3,f4,f5,f6 <-- first row of values 0,10,20,30,40,50,60 <-- second row of values ... best regards

      N Offline
      N Offline
      Nitron
      wrote on last edited by
      #2

      Try my article, CDataFile[^] Just change the std::vector<- Nitron


      "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

      1 Reply Last reply
      0
      • L lbc

        Hello everybody, i am trying to parse a simple CSV file (see a little file sample below) using the functions found in the book 'PROFESSIONAL MFC with VC++6' by M. Blaszczak, Wrox. I am working with VC7. The functions work fine but unfortunately i can get only the first, say, N-1 values parsed instead of all the N wished ones. for example in the sample below i get parsed only: label0 .. label5 as labels row (missing label6) field0,f1,f2,f3,f4,f5 as first row of values (missing field f6) 0,10,20,30,40,50 as second row of values (missing 60) and so on... I mean only six values (with reference to the csv file sample), not all the seven values any help/tip will be gratly appreciated! Thanks in advance class CMyDoc : public CDocument { public: int CountChars(CString& ref, TCHAR ch = ',') const; CString GetField(CString& ref, int nIndex, TCHAR ch = ',') const; ... }; ... int CMyDoc::CountChars(CString& ref, TCHAR ch) const { LPCTSTR pstrBuffer = ref.LockBuffer(); int nCount = 0; while (*pstrBuffer != _T('\0')) { if (*pstrBuffer == ch) nCount++; pstrBuffer++; } ref.UnlockBuffer(); return nCount; } CString CMyDoc::GetField(CString& ref, int nIndex, TCHAR ch) const { CString strReturn; LPCTSTR pstrStart = ref.LockBuffer(); LPCTSTR pstrBuffer = pstrStart; int nCurrent = 0; int nStart = 0; int nEnd = 0; int nOldStart = 0; while (nCurrent <= nIndex && *pstrBuffer != _T('\0')) { if (*pstrBuffer == ch) { nOldStart = nStart; nStart = nEnd+1; nCurrent++; } nEnd++; pstrBuffer++; } ref.UnlockBuffer(); if (*pstrBuffer == _T('\0')) { TRACE("Warning: Couldn't find it.\n"); return strReturn; } return ref.Mid(nOldStart, nEnd-nOldStart-1); } ... // CALLING STUFF ... int nColumns = pDoc->CountChars(pDoc->m_strColumns); TRACE("N. col= %d, header titles= %s\n", nColumns, pDoc->m_strColumns); int nIndex; CString strCurrent; for (nIndex = 0; nIndex < nColumns; nIndex++) { ... print values ... } SAMPLE FILE.CSV label0,label1,label2,label3,label4,label5,label6 <-- labels row field0,f1,f2,f3,f4,f5,f6 <-- first row of values 0,10,20,30,40,50,60 <-- second row of values ... best regards

        N Offline
        N Offline
        Nitron
        wrote on last edited by
        #3

        my message was truncated :confused: Anyway, change the double vector to CString and scrap the statistics stuff and you'll be good to go. - Nitron


        "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

        L 1 Reply Last reply
        0
        • N Nitron

          my message was truncated :confused: Anyway, change the double vector to CString and scrap the statistics stuff and you'll be good to go. - Nitron


          "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

          L Offline
          L Offline
          lbc
          wrote on last edited by
          #4

          thank you very much Nitron! i have just downloaded your excellent code and i'll give it a try as soon as possible anyway i would be really interested in finding the reasons why the code above form Wrox book doesn't work as expected I have tried to debug it and follow all the steps to figure out why the last field isn't parsed but so far i could'f find an answer so i would appreciate very much if anybody could point me towards the solution thanks again! best regards

          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