0D after 0A in hex when writing a binary file

R

Romain

Hello,

I am writing out a binary file. I figured that the number "10" is
automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
line feed and carriage return.

I know it does that if the file is opened in something else than in
binary mode. But in my example below, the file is really opened in
binary.

I am compiling the file with VC++.NET


I am using 010 Editor to edit the binary file.

Thanks for your help,

Ctest2App::Ctest2App()
{
long i;
long Size=10;
LONG len=1;

ofstream *BinFile; // The acq file streams
PUCHAR *buf=new PUCHAR[Size];
BinFile = new ofstream("out.bin");


for (i =0; i < Size; i++) {
buf = new UCHAR[len];
buf[0]=10;
}
for (i =0; i < Size; i++) BinFile->write((char *) buf, len);
}
 
V

Victor Bazarov

Romain said:
I am writing out a binary file. I figured that the number "10" is
automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
line feed and carriage return.

Actually, reverse. "0D" and "0A" are carriage return and line feed
(and those are zeros and not Os in the numbers).
I know it does that if the file is opened in something else than in
binary mode. But in my example below, the file is really opened in
binary.
Nope.


I am compiling the file with VC++.NET


I am using 010 Editor to edit the binary file.

Thanks for your help,

Ctest2App::Ctest2App()
{
long i;
long Size=10;
LONG len=1;

ofstream *BinFile; // The acq file streams
PUCHAR *buf=new PUCHAR[Size];
BinFile = new ofstream("out.bin");

What makes you think this is opening as binary? The fact that you made
the name to contain letters 'b', 'i', and 'n', is definitely not enough.
Do

BinFile = new ofstream("out.bin", ios::binary);

for (i =0; i < Size; i++) {
buf = new UCHAR[len];
buf[0]=10;
}
for (i =0; i < Size; i++) BinFile->write((char *) buf, len);
}


Victor
 
H

Howard

Romain said:
Hello,

I am writing out a binary file. I figured that the number "10" is
automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
line feed and carriage return.

I know it does that if the file is opened in something else than in
binary mode. But in my example below, the file is really opened in
binary.

Really? Isn't the default mode text? The code below doesn't specify binary
anywhere.
I am compiling the file with VC++.NET


I am using 010 Editor to edit the binary file.

Thanks for your help,

Ctest2App::Ctest2App()
{
long i;
long Size=10;
LONG len=1;

ofstream *BinFile; // The acq file streams
PUCHAR *buf=new PUCHAR[Size];
BinFile = new ofstream("out.bin");

Why are you using a pointer here? And where's the specifier saying it
should be binary? I think this should be:

ofstream BinFile("out.bin",ios_base::binary);
for (i =0; i < Size; i++) {
buf = new UCHAR[len];
buf[0]=10;
}
for (i =0; i < Size; i++) BinFile->write((char *) buf, len);
}


(Any reason you're dynamically allocating these buffers? Just curious. If
you need dynamic sizing, you might consider using the string class instead
of dynamic arrays.)

-Howard
 
M

Mike Wahler

Romain said:
Hello,

I am writing out a binary file. I figured that the number "10" is
automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
line feed and carriage return.

The number 10 is the encoding for the 'line feed' character in
ASCII. But note that C++ does not mandate a particular character
set.

Also, such conversions when doing file i/o are platform-specific.
You're describing what happens for MSDOS and MS-Windows, but
C++ can be run with many other operating systems as well, some
of which have different translations, or none at all.

Also, your "OD OA" above is using an upper case letter 'O',
but those should be the digit zero (0).
I know it does that if the file is opened in something else than in
binary mode.

On some platforms. Also note that in C++ there is only one
'something else' besides 'binary mode', which is 'text mode'.
Streams opened in 'text mode' will possibly induce these
translations, *for some platforms*, not all.
But in my example below, the file is really opened in
binary.

Um, no it is not. The default mode for a stream is text.
I am compiling the file with VC++.NET


I am using 010 Editor to edit the binary file.

Thanks for your help,

Ctest2App::Ctest2App()
{
long i;
long Size=10;
LONG len=1;

ofstream *BinFile; // The acq file streams

This is not a stream object, but a pointer to one.
Why are you using a pointer?
PUCHAR *buf=new PUCHAR[Size];
BinFile = new ofstream("out.bin");

This opens the stream in text mode, which will cause translations
on some platforms. BTW why are you dynamically allocating the
stream instead of simply defining one, e.g.

ofstream ofs("out.bin");

You also failed to check whether the open succeeded or failed.
If it failed, any subsequent attempts to use the stream will
certainly fail as well.
for (i =0; i < Size; i++) {
buf = new UCHAR[len];


I don't understand this need you seem to have to dynamically
allocate things. This only makes the code unnecessarily
complex and harder to maintain.
buf[0]=10;
}
for (i =0; i < Size; i++) BinFile->write((char *) buf, len);

}


Which C++ book(s) are you reading?

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top