0

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?

1

1 Answer 1

2

CString::Tokenize can be used to split a string at arbitrary delimiters. In your use case you would pass _T(" ") for the delimiter.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks. I've tried this approach and I feel like I'm closer to the solution. I've been trying to parse the data in the text file properly but I'm missing something somewhere. I've edited the OP, if you could take a look, that'd be great!
break exits the enclosing while loop on the first iteration. Consequently, you are only parsing out the first token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.