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?

nahmiz
  • 1
  • 1
  • Does this answer your question? [Split functionality for MFC Cstring Class](https://stackoverflow.com/questions/3147198/split-functionality-for-mfc-cstring-class) – Andrew Truckle Feb 04 '21 at 05:40

1 Answers1

2

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

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • 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! – nahmiz Feb 03 '21 at 15:50
  • `break` exits the enclosing `while` loop on the first iteration. Consequently, you are only parsing out the first token. – IInspectable Feb 03 '21 at 16:20