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