Search code examples
c++filefile-iowords

Extract words from txt file preceded by a seperator or blank space


try
{
    CStdioFile file(_T("D:\\thedirectory\\1.txt"), CFile::modeRead);
    CString str,mainstr = _T("");

    while(file.ReadString(str))
    {
        mainstr += str;
        mainstr += _T("\r\n");
    }

    CWnd *editwindow = this->GetDlgItem(IDC_EDIT2);
    editwindow->SetWindowText(mainstr);

}
catch(CException* e)
{
    MessageBox(_T("no such file"));
    e->Delete();

}

I have managed to read a .txt file, and then update an edit control box with the contents. works great, but now i want to extract only the 2nd, 3rd, 4th, 5th word separately from the txt file. Any ideas?


Solution

  • Something like:

    int i = 0;
    
    while(file.ReadString(str))
    {   
        i++;
        if (i == 1) {
            mainstr += str;
            mainstr += _T("\r\n");
        }
    }
    

    May be a good place to start experimenting. You can play with the value of i when intializing, placement of the initilizing variable, etc.