Reply embedded...

> -----Original Message-----
> From: plano9 [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, December 28, 2004 9:49 PM
> To: [email protected]
> Subject: [C-Paradise] binary I/O
> 
> Can someone show me how to write the binary string 100001 to a file?

You can't write 6 bits into a file.  The minimum addressable unit in C is a
char, the number of bits are specified in CHAR_BIT.  Unused bits are filled
with zero.

So do you actually want to write the binary value 0x21 (0010 0001b) or the
string that resemble the binary number in printable form (i.e. "100001")?

> I googled something and found lots of tutorials but none really
> explain how to do that very well.
> 
> Here's what I have right now, wrong of course.
> 
> #include <iostream>
> #include <fstream>
> 
> using namespace std;
> 
> int main()
> {
>      ofstream outFile;
>      outFile.open("file.txt", ios::in | ios::binary);

Side notes: 
1.  You should check if open failed.

>      outFile.write(100001,6);

C/C++ do not have notation for binary value.  

IIRC, the first argument is a char*.  Your code actually invokes undefined
behavior.

As I said before, if you want to write a binary value of 0x21, you can use:
    outFile.put(0x21);

If you want to write the string "100001", then you can use:
    outFile.write("100001", 6);

HTH
Shyan





>-----------------------------------------~-~>
CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at 
http://www.eScribe.com/software/C-Paradise/

>------------------------------------------_->


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/C-Paradise/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to