Problem in parsing a file
-
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() { }
-
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() { }
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 ) ;
-
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 ) ;
-
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
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 ) ;
-
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
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]