Checking for valid data in file

J

Joe Van Dyk

I have a file that looks like this:

25.000000 55.000000
23.000000 51.000000
5 8
8700 6000
<and then, at byte 128, starts binary stuff>

What's the proper way to make sure that I'm reading in valid data?
Should I check after each cin that the input stream isn't eof?

Also, if the user gives the program an argument on the command-line that
represents a file name, how do I check (without resorting to
platform-specific languages) that the file exists and is readable? Do I
just try reading it?

Thanks,
Joe
 
J

Joe Van Dyk

Joe said:
I have a file that looks like this:

25.000000 55.000000
23.000000 51.000000
5 8
8700 6000
<and then, at byte 128, starts binary stuff>

What's the proper way to make sure that I'm reading in valid data?
Should I check after each cin that the input stream isn't eof?

Also, if the user gives the program an argument on the command-line that
represents a file name, how do I check (without resorting to
platform-specific languages) that the file exists and is readable? Do I
just try reading it?

Thanks,
Joe

Also, the binary data is a series of structs that have a short, two
unsigned characters, and a long. What's the idiomatic C++ way of
reading that data?
 
V

Victor Bazarov

Joe said:
I have a file that looks like this:

25.000000 55.000000
23.000000 51.000000
5 8
8700 6000
<and then, at byte 128, starts binary stuff>

What's the proper way to make sure that I'm reading in valid data?

What's "valid data"?
Should I check after each cin that the input stream isn't eof?

Probably. What does this have to do with the "binary stuff" that "starts"
at "byte 128"?
Also, if the user gives the program an argument on the command-line that
represents a file name, how do I check (without resorting to
platform-specific languages) that the file exists and is readable? Do I
just try reading it?

Yes, there is no other definition of "readable" in C++. Of course, it is
often enough to successfully open it for reading (without actually reading
it), but you get the idea.

V
 
B

Ben Pope

Joe said:
Also, the binary data is a series of structs that have a short, two

You mean, it's some binary representation of a series of instances of
structs with a short...
unsigned characters, and a long. What's the idiomatic C++ way of
reading that data?

If it's text, create a stream operator for it:

struct mystruct{};

std::istream& operator>>(std::istream& is, mystruct& s) {
// is >> stuff;
return is;
}

If it's binary, then who knows.

Ben Pope
 
J

Joe Van Dyk

Victor said:
What's "valid data"?

"valid data" would be that the file contains 2 doubles followed by 2
doubles followed by 2 unsigned ints followed by 2 unsigned ints.
Probably. What does this have to do with the "binary stuff" that "starts"
at "byte 128"?

Starting at byte 128 in the file, there's a bunch of the following structs:

typedef struct
{
short vtxElev; /* vertex elevation */
u_char vtxZone; /* terrain fearture type */
u_char vtxMono; /* monochrome brightness value */
ulong vtxVrgb; /* rgb color components of vertex */

} CELL_TYPE;

I need to get the rgb color information out of the vtxVrgb elements.
The structs are stored in network-byte order (if it matters).
Yes, there is no other definition of "readable" in C++. Of course, it is
often enough to successfully open it for reading (without actually reading
it), but you get the idea.

Thanks,
Joe
 
V

Victor Bazarov

Joe said:
"valid data" would be that the file contains 2 doubles followed by 2
doubles followed by 2 unsigned ints followed by 2 unsigned ints.

But it doesn't. You just said that somewhere "starts binary stuff".
If the file doesn't end there, it's not "valid data". Your file either
contains formatted stuff or it contains unformatted (binary) stuff. If
it has both, it's not normal.

You could of course read out your 128 (or 127, or whatever) bytes into
a string, and then use that string to initialise an istringstream and
then read those doubles and unsigneds out of the istringstream.
Starting at byte 128 in the file, there's a bunch of the following
structs:
typedef struct
{
short vtxElev; /* vertex elevation */
u_char vtxZone; /* terrain fearture type */
u_char vtxMono; /* monochrome brightness value */
ulong vtxVrgb; /* rgb color components of vertex */

} CELL_TYPE;

I need to get the rgb color information out of the vtxVrgb elements.
The structs are stored in network-byte order (if it matters).

I don't see the problem. You know where in the file your binary data
start, you know what it looks like, so just read it using 'read'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top