473,548 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ 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_cas t 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_cas t 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 9307
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
2547
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 it's buisness. My problem is all the resources I have found aren't very clear on these things. All of them open the file then check to see if the...
7
4204
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, which in matlab is 32 bits. How do I get this binary file in to floats in c++? I try reading the file using the ifstream>>myFloat. But nothing ever...
8
9504
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 contains the following three floating-point numbers: 1.0 2.0 3.0
2
3226
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. // c code typedef struct
2
3162
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 a normal text file. I need to read in a float from the binary file, but it sets my input stream to failbit. Is there a special way I can read in...
2
4275
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 http://sf.gds.tuwien.ac.at/00-pdf/m/mstockfl/MetaStock.pdf
8
4117
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. I have a binary file that has a specific number of floats per row. I need to read each float into a float array for processing by another block...
31
14433
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 that the read and get functions do not accept float type. What should i do now? Can anybody help me please? Here is the code.. #include...
13
3665
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 consists of structures of type---- struct record { int acountnum; char name; float value;
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
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 we have to send another system
0
751
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.