473,480 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading binary float from file

boxfish
469 Recognized Expert Contributor
Hi,
I'm trying to write code in C++ that loads .3ds files. .3ds files use floats in binary for vertex coordinates, but I don't know how to read them. I googled stuff for a bit, until it seemed like reinterpret_cast was the way to do it, but this code
Expand|Select|Wrap|Line Numbers
  1. float readFloat(ifstream& file) {
  2.     char bytes[sizeof(float)];
  3.     for (int i = 0; i < sizeof(float); i++)
  4.         bytes[i] = file.get();
  5.     return reinterpret_cast<float>(bytes);
  6. }
gives me this error:
invalid reinterpret_cast from type `char*' to type `float'
Do any of you know how to read a float from a file? I will appreciate any help you can give me.
Aug 6 '08 #1
8 9299
gpraghuram
1,275 Recognized Expert Top Contributor
After getting the data try to use atof to get the float value.

Raghu
Aug 6 '08 #2
JosAH
11,448 Recognized Expert MVP
Make that last line:

Expand|Select|Wrap|Line Numbers
  1. return *reinterpret_cast<float*>(bytes);
  2.  
kind regards,

Jos
Aug 6 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Errr...
Why can't you just:

Expand|Select|Wrap|Line Numbers
  1. float readFloat(ifstream& file) {
  2.     float bytes;
  3.     file >> bytes;
  4.     return bytes;
  5. }
  6.  
Aug 6 '08 #4
newb16
687 Contributor
Errr...
Why can't you just:
Because it assumes it's ascii string like 1.23e4, not 4 bytes of ieee754
Aug 6 '08 #5
Laharl
849 Recognized Expert Contributor
Which "it" do you mean? The file format or the C++? The C++ most certainly assumes it's 4 bytes of IEEE754.
Aug 6 '08 #6
boxfish
469 Recognized Expert Contributor
Hi,
Thanks for all your replies. This works. Yay.
Expand|Select|Wrap|Line Numbers
  1. float readFloat(ifstream& file) {
  2.     char bytes[sizeof(float)];
  3.     for (int i = 0; i < sizeof(float); i++)
  4.         bytes[i] = file.get();
  5.     float* floatPtr = reinterpret_cast<float*>(bytes);
  6.     return *floatPtr;
  7. }
Well, it compiles anyway. Should load .3ds files when I get my program doing that. Thanks Jos. And hopefully when I post this message I will be upgraded to a member. Hooray.
Aug 6 '08 #7
JosAH
11,448 Recognized Expert MVP
Hi,
Thanks for all your replies. This works. Yay.
Expand|Select|Wrap|Line Numbers
  1. float readFloat(ifstream& file) {
  2.     char bytes[sizeof(float)];
  3.     for (int i = 0; i < sizeof(float); i++)
  4.         bytes[i] = file.get();
  5.     float* floatPtr = reinterpret_cast<float*>(bytes);
  6.     return *floatPtr;
  7. }
Well, it compiles anyway. Should load .3ds files when I get my program doing that. Thanks Jos. And hopefully when I post this message I will be upgraded to a member. Hooray.
Also have a look at the istream::read function to get rid of that silly loop ;-)

kind regards,

Jos
Aug 6 '08 #8
boxfish
469 Recognized Expert Contributor
Alright, the loop's gone.
Aug 6 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2538
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
7
4199
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
8
9496
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
2
3220
by: Ady | last post by:
Hi, I am trying to create an editor and the structure of our existing files are as follows. In C# how would I read the following binary data from a file and convert it into usable data. //...
2
3142
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
2
4269
by: Jack | last post by:
Hi I am having a little trouble trying to read a binary file, I would like to write an ascii to Metastock converter in python but am not having a lot of success. The file formats are ...
8
4113
by: Ronin | last post by:
I'm very new to C++ coming in from using C# and the like. I've read a lot of good posts about binary files but I'm still missing a few key ingredients that will help me with the following problem....
31
14406
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
13
3657
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5357
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.