How can I read data from a text file in tab-delimited format?
The PasteTextFromBuffer can paste the text values from a buffer with tab-delimited data into the grid. The following example demonstrates how you can load data a from a text file:
void CGridSampleView::OnImportTextFile()
{
// pop-up file-open dlg to ask for location
CFileDialog dlgFile(
TRUE,
_T(".txt"),
NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"));
if (dlgFile.DoModal() == IDCANCEL)
return;
CFile textFile;
if (!textFile.Open(dlgFile.GetFileName(),
CFile::modeRead))
{
TCHAR sz[255];
wsprintf(sz, "File %s could not be opened!", dlgFile.GetFileName());
AfxMessageBox(sz);
return;
}
LPTSTR pszBuffer;
DWORD dwSize = textFile.GetLength();
pszBuffer = new TCHAR[dwSize];
textFile.Read(pszBuffer, dwSize);
PasteTextFromBuffer(pszBuffer, dwSize, CGXRange(0,1));
textFile.Close();
delete pszBuffer;
}