title: How to convert a CString to an array of strings in MFC C++?
body: <p>In MFC C++, I'm trying to read a text line by line, and show it in listcontrol.</p>
  <p>The text file is of the format:
  Name Address Rollno Class</p>
  <p>So the text will contain</p>
  <p>John England 25 4</p>
  <p>and so on.</p>
  <p>I want to display them in listcontrol.</p>
  <p>I however, read the files using this code</p>
  <pre><code>if (myFile.Open(_T(&quot;c:\\Users\\blabla\\Desktop\\bla.txt&quot;), 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(&quot;Nope&quot;));
      }
  </code></pre>
  <p>In the above, strLine would print &quot;John England 25 4&quot;
  I want to convert this into an array like [&quot;John&quot;, &quot;England&quot;, &quot;25&quot;, &quot;4&quot;] and then iterate through this array to insert into listcontrol.</p>
  <p>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.</p>
  <p>Edit: I saw that you can tokenize, so this is what I've come up so far</p>
  <pre><code>void CMFCTask2Try2Dlg::OnBnClickedButton1()
  {
      // TODO: Add your control notification handler code here
      CStdioFile myFile;
      CString strLine;
      CString strMsg;
   
      if (myFile.Open(_T(&quot;c:\\Users\\blabla\\Desktop\\nana.txt&quot;), CFile::modeRead))
      {
          while (myFile.ReadString(strLine))
          {
              strMsg += strLine + '\n'
   
              char myString[256];
   
   
              CAtlString str(strLine);
              CAtlString resToken;
   
              int curPos = 0;
              resToken = str.Tokenize(_T(&quot; &quot;), curPos);
              int i = 0;
              int nItem;
              while (resToken != _T(&quot;&quot;))
              {
                  _tprintf_s(_T(&quot;Resulting token: %s\n&quot;), resToken);
   
                  AfxMessageBox(resToken, MB_ICONINFORMATION);
                  nItem = mcontrolz.InsertItem(i, resToken);
   
                  resToken = str.Tokenize(_T(&quot; &quot;), curPos);
                  break;
                  //Here resToken prints
              }
          }
      }
      else
      {
          AfxMessageBox(_T(&quot;Nope&quot;));
      }
   
      //myFile.Close();
  }
  </code></pre>
  <p>so if my text file  has only</p>
  <pre><code>hey how are you
  </code></pre>
  <p>In this line of code</p>
  <pre><code>AfxMessageBox(resToken, MB_ICONINFORMATION);
  </code></pre>
  <p>The messagebox would give me only &quot;hey&quot;  works great so far but if my text file is</p>
  <pre><code>hey how are you
  hi how are you 
  </code></pre>
  <p>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?</p>