473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

binary i/o question

I've developed a templated binary read/write routine:

template<class T>
void write(ofstream& out,T data){
out.write((char*)&data,sizeof(T));
}

template<class T>
void read(ifstream& in,T& data){
in.read((char*)(&data),sizeof(T));
}
Which I call in a function like:

ofstream out;
out.open("restart.solv",ios::out|ios::binary);
write(out,var);
out.close();

ifstream in;
in.open("restart.solv",ios::in|ios::binary);
read(in,var);

Where var is a double...
When I read it back in however, it only retains 6 digits of accuracy.
Any ideas as to what might be causing this?

Thanks!

Matt
Jul 27 '06 #1
2 1049
Matt Jackson wrote:
I've developed a templated binary read/write routine:

template<class T>
void write(ofstream& out,T data){
out.write((char*)&data,sizeof(T));
}

template<class T>
void read(ifstream& in,T& data){
in.read((char*)(&data),sizeof(T));
}
Which I call in a function like:

ofstream out;
out.open("restart.solv",ios::out|ios::binary);
write(out,var);
out.close();

ifstream in;
in.open("restart.solv",ios::in|ios::binary);
read(in,var);

Where var is a double...
When I read it back in however, it only retains 6 digits of accuracy.
How do you know that?
Any ideas as to what might be causing this?
I think the cause is in the part of the program you *didn't* post.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #2
Matt Jackson wrote:
Where var is a double...
When I read it back in however, it only retains 6 digits of accuracy.
Any ideas as to what might be causing this?
I'd bet on "perhaps it is read in correctly, yet rounded to
6 digits during output only?", but this is a wild guess since
you didn't post code that would demonstrate the problem.

HTH anyway,
- J.
Jul 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative...
1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
5
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
9
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
15
by: miki | last post by:
hello, I was asked this question during a job interview, and didn't know the answer. could someone please help me with that? the question was: "Which files are more suitable for working in C,...
15
by: mleaver | last post by:
I want to open a second window and display a binary image that is returned from a java program via XMLRPC. The data returned is a binary encoded base64 png file. If I write the data out to a file...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
19
by: sethukr | last post by:
Hi everybody, Can we assign binary values to an integer variable like assigning Hexadecimal values??? Thanks, Sethu
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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
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...
0
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,...

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.