Question

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?

Était-ce utile?

La 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.

Autres conseils

You could use a find method from CString to find a "word separating" character, then use a substring method to extract the word.

Search StackOverflow for "CString parse" or "CString regular expression".

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top