# Read and Write Both Integers and Floats?

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

## 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?

std::ifstream File( cFileName);

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

}

// 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 on other sites
You are going to need some kind of file structure. What do the integers or floats represent?

##### 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 on other sites
Quote:
 Original post by the_edd1. Use std::vector. 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 on other sites
Quote:
 Original post by the_edd1. Use std::vector. 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 on other sites
Quote:
 Original post by 51monI 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 on other sites
Quote:
Original post by Promethium
Quote:
 Original post by 51monI 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 on other sites
Quote:
Original post by Promethium
Quote:
 Original post by 51monI 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.

##### Share on other sites
What would happen if you used a data aggregate of floats/ints and stored that as binary?

--random

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
13
5. 5

• 9
• 13
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007350

×