Read and Write Both Integers and Floats?

Started by
7 comments, last by random_thinker 16 years, 7 months ago
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));
}


Advertisement
You are going to need some kind of file structure. What do the integers or floats represent?
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
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]
---Sudet ulvovat - karavaani kulkee
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?
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.
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.

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
What would happen if you used a data aggregate of floats/ints and stored that as binary?

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.

This topic is closed to new replies.

Advertisement