std::oftream and Binary 
Author Message
 std::oftream and Binary

What is going on with the streams????
I open the stream for binary output but it only writes in text
mode.
  std::ofstream OutFile(FileName.c_str(), ios_base::out|
ios_base::binary | ios_base::trunc);

int data = 1;
Tried OutFile << data;
And   OutFile.write((char*)&data, sizeof(data));

The output is just a text "1" not binary.



Mon, 03 Dec 2001 03:00:00 GMT  
 std::oftream and Binary
On Thu, 17 Jun 1999 12:00:17 -0400, "Mark Levis"

:What is going on with the streams????
:I open the stream for binary output but it only writes in text
:mode.
:  std::ofstream OutFile(FileName.c_str(), ios_base::out|
:ios_base::binary | ios_base::trunc);
:
:int data = 1;
:Tried OutFile << data;
:And   OutFile.write((char*)&data, sizeof(data));
:
:The output is just a text "1" not binary.

Were you expecting "0000001" instead? The binary input/output doesn't
mean you're actually going to the binary representation of a
character, all it means is that newlines are a single character, not a
carriage return and linefeed, which is two characters.



Thu, 06 Dec 2001 03:00:00 GMT  
 std::oftream and Binary
No I am expecting a four byte output for an int (since it is 4 bytes) on
a win32 platform. The same as one would get using
write(const_cast<char*>(&data), sizeof(data); which would be 0x01 0x00
0x00 0x00 (of course not actually this way this is readable and space
delimited).

I should not have to code this using write() and casting each variable
to char *.

Quote:

> On Thu, 17 Jun 1999 12:00:17 -0400, "Mark Levis"

> :What is going on with the streams????
> :I open the stream for binary output but it only writes in text
> :mode.
> :  std::ofstream OutFile(FileName.c_str(), ios_base::out|
> :ios_base::binary | ios_base::trunc);
> :
> :int data = 1;
> :Tried OutFile << data;
> :And   OutFile.write((char*)&data, sizeof(data));
> :
> :The output is just a text "1" not binary.

> Were you expecting "0000001" instead? The binary input/output doesn't
> mean you're actually going to the binary representation of a
> character, all it means is that newlines are a single character, not a
> carriage return and linefeed, which is two characters.



Sat, 08 Dec 2001 03:00:00 GMT  
 std::oftream and Binary
I want to clarify what I was implying below

Quote:

> No I am expecting a four byte output for an int (since it is 4 bytes) on
> a win32 platform. The same as one would get using
> write(const_cast<char*>(&data), sizeof(data); which would be 0x01 0x00
> 0x00 0x00 (of course not actually this way this is readable and space
> delimited).

The above would be output on a machine that had 4 byte integers and is a
big endian machine. Just want to stop the flames before they ae sent.
Quote:
> I should not have to code this using write() and casting each variable
> to char *.


> > On Thu, 17 Jun 1999 12:00:17 -0400, "Mark Levis"

> > :What is going on with the streams????
> > :I open the stream for binary output but it only writes in text
> > :mode.
> > :  std::ofstream OutFile(FileName.c_str(), ios_base::out|
> > :ios_base::binary | ios_base::trunc);
> > :
> > :int data = 1;
> > :Tried OutFile << data;
> > :And   OutFile.write((char*)&data, sizeof(data));
> > :
> > :The output is just a text "1" not binary.

> > Were you expecting "0000001" instead? The binary input/output doesn't
> > mean you're actually going to the binary representation of a
> > character, all it means is that newlines are a single character, not a
> > carriage return and linefeed, which is two characters.



Sat, 08 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Loadable Binary Modules in Std C

2. Writing binary values with std::basic_ostream

3. Proposal: alt.binaries.examples.vb4 , alt.binaries.examples.vcpp , alt.binaries.examples.java

4. No std::min or std::max?

5. bug: VS7.0 (6.0) C++ std::auto_ptr conflict with std::vector

6. convert between std::string and std::wstring

7. convert non std::string to std::string

8. Warnings for std::vector<std::string>

9. export classes using std namespace (ex std::vector) in a DLL

10. typedef std::vector<std::string> Vector_String

11. VC6, STL, std::set_new_handler and std::bad_alloc

12. std::cout and std::cerr

 

 
Powered by phpBB® Forum Software