Sign in to follow this  

Read and Write Both Integers and Floats?

This topic is 3747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to write and read both integers and floats to the same binary file on disc. When doing just integers it’s easy, I just open the file and use read and write. Does anyone have any recommendations?
// How does I extend this to handle floats to?

// Reading
std::ifstream File( cFileName);

if( File)
{
   int* pInput = new int[COUNT];
   File.read( (char*)pInput, COUNT * sizeof(int));

   // Do the reading

}


// Writing
std::ofstream File( cFileName, std::ios::binary);

if( File)
{
   int* pOutput = new int[COUNT];

   // Do the writing

   File.write( (char*)pOutput, COUNT * sizeof(int));
}


Share this post


Link to post
Share on other sites
1. Use std::vector<int>. You have two memory leaks even in your tiny snippets. It may be the case that delete[]s exist in your real code, but by not using a vector<> or similar, you're asking for trouble. The fact that COUNT is capitalised seems to suggest that it is a constant, in which case, you shouldn't need dynamic allocation at all?

2. You can write floats in the same way as ints.

But, different machines will interpret these differently. The same is true of ints, though.

If your files only have to be interpreted by a single kind of machine, just use ostream::write and read in the same way.

However, if your files have to be read by a variety of architectures, you'll need to decide how to decompose your floats in to integral chunks (significand and exponent, or whatever), or standardise on a particular floating point format and make sure all clients can read and write that format (edit: e.g. IEEE 754).

Edd

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
1. Use std::vector<int>. You have two memory leaks even in your tiny snippets. It may be the case that delete[]s exist in your real code, but by not using a vector<> or similar, you're asking for trouble. The fact that COUNT is capitalised seems to suggest that it is a constant, in which case, you shouldn't need dynamic allocation at all?

2. You can write floats in the same way as ints.

But, different machines will interpret these differently. The same is true of ints, though.
Quite right. As an interesting side note to the interested people is that in banking software everything is just huge integers written in character strings in hexadecimal. Everything that goes to the wire or any store another than the in-memory structures of the program, must be in this format. If not, audition will fail. Well, at least in card transactions this is true. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
1. Use std::vector<int>. You have two memory leaks even in your tiny snippets. It may be the case that delete[]s exist in your real code, but by not using a vector<> or similar, you're asking for trouble. The fact that COUNT is capitalised seems to suggest that it is a constant, in which case, you shouldn't need dynamic allocation at all?

2. You can write floats in the same way as ints.

But, different machines will interpret these differently. The same is true of ints, though.

If your files only have to be interpreted by a single kind of machine, just use ostream::write and read in the same way.

However, if your files have to be read by a variety of architectures, you'll need to decide how to decompose your floats in to integral chunks (significand and exponent, or whatever), or standardise on a particular floating point format and make sure all clients can read and write that format (edit: e.g. IEEE 754).

Edd


Yeah, I'm deleting too, just cropped the snippet a bit. The files are just going to be used on a single machine (it's a very small script to a demo). I don't really understand how to handle read. Say that I first read the integers then when I read the floats doesn't that command starting over again. I think I need to traverse the stream with some binary type that could be converted to both integers and floats?

Share this post


Link to post
Share on other sites
Quote:
Original post by 51mon
I don't really understand how to handle read. Say that I first read the integers then when I read the floats doesn't that command starting over again. I think I need to traverse the stream with some binary type that could be converted to both integers and floats?

No the stream keeps an internal pointer to the current position in the file. That pointer is updated every time you read something, so you get something new every time you call read, until you hit eof.

Share this post


Link to post
Share on other sites
Quote:
Original post by Promethium
Quote:
Original post by 51mon
I don't really understand how to handle read. Say that I first read the integers then when I read the floats doesn't that command starting over again. I think I need to traverse the stream with some binary type that could be converted to both integers and floats?

No the stream keeps an internal pointer to the current position in the file. That pointer is updated every time you read something, so you get something new every time you call read, until you hit eof.
Yes. But of course you need to know if each element you're about to read is an int or a float. If you have all ints first then all floats, you could put right at the start of the file the number of each you expect.

Share this post


Link to post
Share on other sites
Quote:
Original post by Promethium
Quote:
Original post by 51mon
I don't really understand how to handle read. Say that I first read the integers then when I read the floats doesn't that command starting over again. I think I need to traverse the stream with some binary type that could be converted to both integers and floats?

No the stream keeps an internal pointer to the current position in the file. That pointer is updated every time you read something, so you get something new every time you call read, until you hit eof.

Yeah that’s correct. I was led into the misconception because of other errors in the code.


Thank you all for your comments :D

Share this post


Link to post
Share on other sites

This topic is 3747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this