Re: reading text file with MFC solution
No error checking and probably with some "off by 1" errors but this is what
I came up with. Any comments are welcome. Thank you!
CStdioFile stdFile;
CFileException CFileEx;
if(!stdFile.Open(_T("C:\\products.txt"), CFile::modeNoTruncate |
CFile::modeRead| CFile::modeCreate, &CFileEx))
{
CString strLine;
CString strBarCode;
CString strName;
CString strQty;
CString strPrice;
while (stdFile.ReadString(strLine))
{
if (strLine.GetLength() == 0)
continue;
strLine.Trim();
int iPos = strLine.Find(_T(' '), 0);
int i = 0;
if (iPos != -1)
{
for (i = 0; i < iPos; ++i)
strBarCode += strLine.GetAt(i);
}
int iEndOfBarcode = i;
int iWhiteSpace = strLine.ReverseFind(_T(' '));
int iLineLength = strLine.GetLength();
for (i = iWhiteSpace+1; i < iLineLength; ++i)
strPrice += strLine.GetAt(i);
while (strLine.GetAt(iWhiteSpace) == _T(' '))
--iWhiteSpace; // get to end qty
int iEndQty = iWhiteSpace;
while (strLine.GetAt(iWhiteSpace) != _T(' '))
--iWhiteSpace; // get to begin qty
int iBeginQty = iWhiteSpace;
for (i = iBeginQty; i < iEndQty; ++i)
strQty += strLine.GetAt(i);
while (strLine.GetAt(iBeginQty) == _T(' '))
--iBeginQty; // get to the end of product name
while (strLine.GetAt(iEndOfBarcode) == _T(' '))
++iEndOfBarcode; // get to beginning of product name
for (int i = iEndOfBarcode; i < iBeginQty; ++i)
strName += strLine.GetAt(i);
}
}
stdFile.Close();
Dan