Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dragon376

Read float from binary file

This topic is 6273 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

How can I read (and write) data such as floats, int, etc to and from a binary file in C++. I was trying to use streambuf::sgetn() but it won''t let me cast a char[4] to a float. Any ideas? Dragon376

Share this post


Link to post
Share on other sites
Advertisement
  
ofstream out;
float f = value;
out.open("filename", ios::binary);
out.write((char*)&float, sizeof(float));
out.close();
.
.
.
.
.
ifstream in;
float f;
in.open("filename", ios::binary);
in.read((char*)&float, sizeof(float));
in.close();

Share this post


Link to post
Share on other sites
isn''t that supposed to be

  
out.write((char*)&f, sizeof(float));

and the same for the reading in?
Otherwise where do you use ''f''?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ironfroggy
wouldnt the overloaded << and >> operators do the job?



No, they would save a textual representation of the float; eg something like ''3'', ''1'', ''4'', ''1''. ''5'', ''9''.

Share this post


Link to post
Share on other sites
I tried this binary in/output out and it all works, except with char* ! I've been hammering on it for hours and it just won't work!

I wrote 2 simple programs to test it out:
  
// cain.cpp

#include <fstream>
#include <iostream>

int main()
{
char* tmp;

ifstream ifs("ca", ios::binary | ios::in); // the extra flag is due to a bug in gcc up til v2.95

ifs.read((char*)&tmp, sizeof(char)*8);
ifs.close();

cout << tmp << endl;
return 0;
}

  
// caout.cpp

#include <fstream>
#include <iostream>

int main(int argc, char* argv[])
{
char* tmp = "tjip";

ofstream ofs("ca", ios::binary | ios::out);
ofs.write((char*)&tmp, sizeof(char)*8);
ofs.close();


return 0;
}

The only thing it does is return a string used somewhere before, so it's probably garbage it reads in...

Please help me out, I'm desperate!


Edited by - jedyte on September 8, 2001 1:04:20 PM

Edited by - jedyte on September 8, 2001 1:06:24 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
char* is already a pointer, so you don''t need to take the adress of it.

Share this post


Link to post
Share on other sites
yes, my bad.....it should be &f, instead of &float.
But it still should be sizeof(float), or sizeof(f) would work too, but can be tricky sometimes when the variable is a pointer.


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
This code will work:
  
#include <fstream.h>
#include <iostream.h>

#define FUNC_OUTPUT //disable this line for reading


#ifndef FUNC_OUTPUT
int main()
{
char tmp[8];

ifstream ifs("ca", ios::binary | ios::in); // the extra flag is due to a bug in gcc up til v2.95

ifs.read((char*)tmp, sizeof(char)*8);
ifs.close();

cout << tmp << endl;
return 0;
}
#else
int main(void)
{
char* tmp = "tjip";

ofstream ofs("ca", ios::binary | ios::out);
ofs.write((char*)tmp, sizeof(char)*8);
ofs.close();


return 0;
}
#endif


Your problem is that you took the adress of the char*. But a char* or char[] is already a pointer. Another problem was that you wanted to read to a char*, which pointed to no specific memory. When you declare it "char tmp[8];" you make the compiler reserve 8 chars of your memory into which it can read the data.

Hope that answers your question.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!