문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top