473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking for valid data in file

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
Mar 17 '06 #1
5 2590
Joe Van Dyk wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 17 '06 #2
Joe Van Dyk wrote:
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?

Mar 17 '06 #3
Joe Van Dyk wrote:

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
--
I'm not just a number. To many, I'm known as a string...
Mar 17 '06 #4
Victor Bazarov wrote:
Joe Van Dyk wrote:
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"?


"valid data" would be that the file contains 2 doubles followed by 2
doubles followed by 2 unsigned ints followed by 2 unsigned ints.
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"?


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).
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.


Thanks,
Joe
Mar 17 '06 #5
Joe Van Dyk wrote:
Victor Bazarov wrote:
Joe Van Dyk wrote:
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"?


"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.
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"?


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
--
Please remove capital As from my address when replying by mail
Mar 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tek9_AK | last post by:
I have a function which reads a csv file into an array from there I want to check if a value is in it. Basically the csv files look like this: 23123233123,name,some@one.com...
7
by: - ions | last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie. M1,M2,M3.......throgh too M110 (thes are the messier objects, a catolouge of deep sky objects) the user selects of of these...
1
by: John S | last post by:
Hi All, I have several questions about checking backup file : a. When SQL Server receive a command : BACKUP DATABASE test to DISK = '\\pc1\netdb\BTEST', does it verify BTEST file before...
2
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data"...
2
by: MT | last post by:
Hi, I am currently validating an XML file against a Schema using XMLValidatingReader. The schema actually contains ranges for particular elements and I have been using it to detect range errors...
30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
6
by: Abubakar | last post by:
Hi, lets say I have a connected SOCKET s. At some point in time, I want to know if the "s" is still valid, that it is still connected. Is there any API that I can give me this information? And...
1
by: DSmith1974 | last post by:
Hi All, Can anyone help with the following problem? I have a database which contains a table with a 'text' field, and the text field contains an xml document - typically 50-100K. Now I'd like...
6
by: Daniel Padron | last post by:
Ok. Maybe I shouldnt post such basic questions here in such an advanced group but my high school programming teacher wont answer any questions outside of his curriculum :( My goal is create a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.