Re: reading text file with MFC solution

Tech-Archive recommends: Fix windows errors by optimizing your registry



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


.



Relevant Pages

  • Re: Inheritance
    ... CString GetTeammascot(); ... int FindTeam; // Get the index for a particular team given their team name by looping through list ... class glider: public monoplane { ... Given this hierarchy, I can probably come up with actual planes that don't fit it. ...
    (microsoft.public.vc.mfc)
  • Re: Newbie: out of memory issues
    ... Heres the problem code:: Lots of array handling with objects.. ... You should use CString. ... char out2; ... BOOL swe_date_conversion(int year, int month, int day, int hour, int cal, double & ...
    (microsoft.public.vc.mfc)
  • Re: reading text file with MFC solution
    ... CString strLine; ... CString strBarCode; ... int iPos = strLine.Find, ... int iWhiteSpace = strLine.ReverseFind); ...
    (microsoft.public.vc.mfc)
  • Re: How To Refresh tree
    ... > pane tree control to Right pane list control which contain Tree Item, ... > CString strItemQry; ... > int nItemPriKey = GetItemPrimaryKey; ... > // check for any sub chield under sub chield ...
    (microsoft.public.vc.mfc)
  • Re: CStringArray
    ... void InsertName(int modNum, int location, const CString & name) ... ATLASSERT(ValidIndexes(modNum, location)); ... CString GetName(int modNum, int location) const ...
    (microsoft.public.vc.mfc)