• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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.
0
jaroslav
Asked:
jaroslav
  • 5
  • 3
  • 3
  • +3
1 Solution
 
allymCommented:
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.  
0
 
nietodCommented:
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.
0
 
KangaRooCommented:
Probably no easy way. You'll have to labouriously read each field and check it is valid.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
laeuchliCommented:
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.
0
 
jaroslavAuthor Commented:
for which type of field shall I use what command to check (e.g. integer = strtol(*a, **b, i)?
0
 
KangaRooCommented:
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.
0
 
KangaRooCommented:
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
0
 
KangaRooCommented:
forgot the arguments:
  char str_field1[13];
  char str_field3[26];
  int  int_field2;
scanf("%12c%8d%25c", str_field1, int_field2, str_field3);
0
 
ozoCommented:
What would you consider to be a valid integer?  What dates would you consider to be invalud?
0
 
jaroslavAuthor Commented:
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.
0
 
nietodCommented:
use sscanf() for that.
0
 
KangaRooCommented:
There are some variations on scanf():

fscanf      scans and formats input from a stream
scanf      scans and formats input from stdin
sscanf      scans and formats input from a string
vfscanf      scans and formats input from a stream, using an argument list
vscanf      scans and formats input from stdin using an argument list
vsscanf      scans and formats input from a string, using an argument list

The best way to go about is probably to use fgets to read a line into a buffer, then parse the string with sscanf()
0
 
jaroslavAuthor Commented:
Good answer, KangaRoo tried till I understood!
0
 
nietodCommented:
Note that the versions that begin with a "v" are non-standard.  You might not have them on your compiler.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now