Dec 8, 2021 at 2:47am UTC
I am trying to read only txt file word by word. I succeed reading txt file and add whole contexts(txt file contexts) to listbox(m_list2).
I want to use fscanf() and save words by array using while loop, because I have to use them for calculation later. Please, give any idea for this code.
[code]
void CFileloadView::OnBnClickedButton1()
{
CFileDialog dlg(TRUE, _T("*.txt"), NULL, OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT, _T("TXT Files(*.txt)|*.txt|"), NULL);
if (dlg.DoModal() == IDOK)
{
CStdioFile rFile;
CString strBufferLine;
int count = 0;
int num;
if (!rFile.Open(dlg.GetPathName(), CFile::modeRead))
{
MessageBox(_T("Can't OpenFile!"), _T("Warning"), MB_OK | MB_ICONHAND);
return;
}
while (rFile.ReadString(strBufferLine))
{
count++;
m_list2.AddString(strBufferLine);
strBufferLine.Replace(("\r"), (""));
if (strBufferLine.GetAt(0) == '#')
continue;
}
rFile.Close();
}
}
Last edited on Dec 8, 2021 at 7:31am UTC
Dec 8, 2021 at 7:49am UTC
You can use standard C++ iostreams to read the file. I've never found MFC file "helpers" helpful.
Dec 11, 2021 at 9:07am UTC
https://www.geeksforgeeks.org/cpp-program-read-file-word-word/. I hope this link will be useful for you.