Re: reading text file with MFC



On 22 jul, 13:44, "Dan" <d...@xxxxxxxxx> wrote:
Hi,

This is a newbie question so I hope somebody can help me. I have a text file
with 4 columns delimited by whitespaces. They basically represent a barcode,
a product name, a quantity and a price.

Here is an example:

342432324342    Name of Product           20              12.78

So, I am trying to read this file line by line, extract each column and
process it. Either I am missing something, or this is kind of complicated..
This is my idea of doing this so far:

 CStdioFile stdFile;
 CFileException CFileEx;

 if(!stdFile.Open(_T("C:\\products.txt"), CFile::modeNoTruncate |
CFile::modeRead | CFile::modeCreate, &CFileEx))
 {
  while (stdFile.ReadString(strLine)) // read line by line
  {
   if (strLine.GetLength() == 0)
    continue; // if the line is blank, ignore it and move to the next

   strLine.Trim(); // get rid of leading and trailing whitespaces

   int iPos = strLine.Find(_T(' '), 0); // where is the end of the first
field ?

   if (iPos != -1)
   {
    for (int i = 0; i < iPos; ++i)
     strBarCode += strLine.GetAt(i); // get barcode.. char by char, could I
do it more efficiently?
   }

   while (strLine.GetAt(iPos) == _T(' '))
    ++iPos; // move across all the whitespaces and onto the beginning of the
next field

   int iWhiteSpace = strLine.Find(_T(' '), iPos); // find the next white
space (could be part of the name so must find out later)

   for (int i = 0; i < iWhiteSpace; ++i)
    strName += strLine.GetAt(i); // get all the name or part of the name,
unknown yet

   TCHAR tchr = strLine.GetAt(iWhiteSpace + 1); // trying to find out if
this is a digit or a text so i determine if it is still the name or the
quantity

Question here: What do I do if the product name contains a digit ? Such as
"Product Name 2".. there is no way to distinguish this from the quantity,
unless I probably read the string from the end to the beginning, correct ?
Even then though, there is a problem.. what if the line looks like this:
"342432324342   2 Name of Product    20      12.78", where the name of the
product is "2 Name of Product".. that basically means that even when I go
from the end to the beginning, to correctly retrieve the number of the
barcode I would have to count the characters to the "beginning" from the
first 'number' just to make sure that the first digit is not part of the
name.. right ? Are there any other potential problems that you may see ?

So, maybe this is the solution.. to read the string from the end, to the
beginning. Am I missing something or am I on the right track ? Should I do
it differently to be more efficient or is this OK?
 }

Thanks for your time!

First of all, take a look at Tokenize function. It's easier than
locating the separators yourself.

Second, if you allow your separator to appear in any of the fields,
you are going to run into trouble. So, instead of using whitespaces as
separators, why not commas, semicolons, tabs? Another option would be
to "escape" those characters, but then you would probably have to
parse the line yourself, character by character. And another one, to
add a "product name length" field.

If you don't have any control over the file format, what I would do,
probably, is to read the barcode number first, then the price, then
the quantity, and what's left would be the product name.

Just my 2 cents
.



Relevant Pages

  • reading text file with MFC
    ... This is a newbie question so I hope somebody can help me. ... with 4 columns delimited by whitespaces. ... What do I do if the product name contains a digit? ... first 'number' just to make sure that the first digit is not part of the ...
    (microsoft.public.vc.mfc)
  • Re: Postroad Mailer message file name algorithm
    ... possible filename characters, 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ. ... The first digit of a *.POP filename is the last digit of the current ... into that array of characters. ...
    (de.comp.os.os2.networking)
  • RE: Whitespace in passwords
    ... characters (including whitespaces). ... > Audit your website security with Acunetix Web Vulnerability Scanner: ... Up to 75% of cyber attacks are launched on shopping carts, ...
    (Pen-Test)
  • Re: Need help solving a puzzle. Please.
    ... that the ciphertext does not consist of column/row pairs. ... There are 480 alphabetic characters in the passage. ... Breaking the ciphertext into three digit groups, ... Looking at the 96 words available, the first letters do not yield ...
    (sci.crypt)
  • Keystoke events
    ... I am creating a page that has a number of single digit text fields. ... and where any special characters are to be placed. ... output the typed character ... "+" gives a strange arrow character, and none of the swedish extra ...
    (microsoft.public.scripting.jscript)