Link to home
Start Free TrialLog in
Avatar of jaroslav
jaroslav

asked on

validate

I have to validate orders from a flat file (ASCII, fixed length). This file contains different fields: integer, double, float, date, time. What's the best way to check, if all these fields just contain valid data (a valid integer, a valid date, ...)?

Thank's a lot.
Avatar of allym
allym

I'd use a tool suited to the job: Perl.

But if you can't learn a new language, or are tied to C++ for the rest of your system, there are various parsers that can help.  I've not done any myself, but LEXX and YACC are tools that my team have used in the past.  
Is this for a school assignment?


You will have to read each record and test each field "manually"  It is probably best to read the record in as an ASCII string, then examine the seperate fields inside the string.  (Probably best to read the record using the GetLine() function.)  

That's about all anyone can say unless yoiu give us mre details.
Probably no easy way. You'll have to labouriously read each field and check it is valid.
The way I would do it is spilt the file up with c++ then parse it with  awk. I do this all the time. If you can't do that shoot yourself. You are in for the ultimate in programming bordom.
Avatar of jaroslav

ASKER

for which type of field shall I use what command to check (e.g. integer = strtol(*a, **b, i)?
Preferable iostreams or sscanf(). Do not use strtol and its companions because it returns a 0 on error. A 0 is usually a valid number.
scanf type specifiers:
integer:      %d
long:            %ld
float:            %f
double:            %lf
long double:      %Lf
char*            %s
Most allow addotional width specification
Check out the manual on scanf, there are so many possiblities. For simple flat files it might even be possible to scan an entire line in one step, for example:
scanf("%12c%8d%25c");
could be used to scan the format
  12 chars
  8 digit number
  25 characters
forgot the arguments:
  char str_field1[13];
  char str_field3[26];
  int  int_field2;
scanf("%12c%8d%25c", str_field1, int_field2, str_field3);
Avatar of ozo
What would you consider to be a valid integer?  What dates would you consider to be invalud?
valid integer: just numeric values (0-9), without point

valid date: date in different formats,
but checking for correct day, month and leap year

Answer from KangaRoo sounds good, but does not solve the problem, because scanf from stdin is not the same for me as reading from file.

I read as follows:
do
{
ReadFile(hFile, pBuffer, sizeof(pBuffer) -1, &fRead, NULL);
pBuffer[fRead] = '\0';
if (fRead == 0)
      break;
} while (fRead > 0);

So I have pBuffer to check.
use sscanf() for that.
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good answer, KangaRoo tried till I understood!
Note that the versions that begin with a "v" are non-standard.  You might not have them on your compiler.