#include// ... CStdioFile file; CString str; if (file.Open(_T("data.txt"), CFile::modeRead)) { file.ReadString(str); file.Close(); } // use str here...
#includeIn this example, the MFC `CFileDialog` is used to prompt the user to select a file to open. Once a file is selected, CStdioFile is used to read the contents of the file into a CString object as in the first example. The library package for CStdioFile is the MFC (Microsoft Foundation Classes) library, which is part of the Microsoft Visual C++ development environment.// ... CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||")); if (dlg.DoModal() == IDOK) { CString path = dlg.GetPathName(); CStdioFile file; CString str; if (file.Open(path, CFile::modeRead)) { file.ReadString(str); file.Close(); } // use str here... }