In MFC C++, I'm trying to read a text line by line, and show it in listcontrol.
The text file is of the format: Name Address Rollno Class
So the text will contain
John England 25 4
and so on.
I want to display them in listcontrol.
I however, read the files using this code
if (myFile.Open(_T("c:\\Users\\blabla\\Desktop\\bla.txt"), CFile::modeRead))
{
while (myFile.ReadString(strLine))
{
strMsg += strLine + '\n';
int lengtha = (strLine).GetLength();
//SetDlgItemText(IDC_LIST3, strLine);
char myString[256];
AfxMessageBox(strLine, MB_ICONINFORMATION);
//How do I iterate through strLine?
}
}
else
{
AfxMessageBox(_T("Nope"));
}
In the above, strLine would print "John England 25 4" I want to convert this into an array like ["John", "England", "25", "4"] and then iterate through this array to insert into listcontrol.
I'm new to MFC C++, and I can't seem to understand how to do this at all. If anyone can help, that'd be great.
Edit: I saw that you can tokenize, so this is what I've come up so far
void CMFCTask2Try2Dlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CStdioFile myFile;
CString strLine;
CString strMsg;
if (myFile.Open(_T("c:\\Users\\blabla\\Desktop\\nana.txt"), CFile::modeRead))
{
while (myFile.ReadString(strLine))
{
strMsg += strLine + '\n'
char myString[256];
CAtlString str(strLine);
CAtlString resToken;
int curPos = 0;
resToken = str.Tokenize(_T(" "), curPos);
int i = 0;
int nItem;
while (resToken != _T(""))
{
_tprintf_s(_T("Resulting token: %s\n"), resToken);
AfxMessageBox(resToken, MB_ICONINFORMATION);
nItem = mcontrolz.InsertItem(i, resToken);
resToken = str.Tokenize(_T(" "), curPos);
break;
//Here resToken prints
}
}
}
else
{
AfxMessageBox(_T("Nope"));
}
//myFile.Close();
}
so if my text file has only
hey how are you
In this line of code
AfxMessageBox(resToken, MB_ICONINFORMATION);
The messagebox would give me only "hey" works great so far but if my text file is
hey how are you
hi how are you
The resToken here AfxMessageBox(resToken, MB_ICONINFORMATION); at this line would be give two messagebox alerts, one being hey and one being hi no idea why it gives me that. Can someone help me to figure this out?