Topic: Help! - ouput 0x0A translated to 0x0d0a
Author: feb6399@ultb.isc.rit.edu (F.E. Barrus)
Date: Wed, 15 Mar 1995 18:40:41 GMT Raw View
In article <sehlke.2.2F60A7BF@cts.com> sehlke@cts.com (Steve Ehlke) writes:
>This question is about the writing of individual characters to a file.
>I am trying to create a binary file, and each time I write the character 0A
>(Hex) it is translated into the pair 0D0A (Hex). I have tried putc, fputc,
>fwrite, and putw. There must be a way to write 0A without it being
>translated into a 0D0A pair. I am using DOS with two different Borland
>compilers. Borland C++ 4.5 and Borland C++ 2.0. They both act the same way.
Trying opening the file with "wb" instead of "w" for the mode.
And for reading, use "rb" instead of "r".
DOS (and also VMS) always keep CR/LF pairs together in the file
to denote the equivalent of the Unix newline (just LF), so the
file streams automatically translate this for compatability.
Therefore they have to have a separate binary mode.
I'm not sure if this really belongs in this newsgroup though,
since it refers to a DOS/VMS problem not a C++ standard,
so in the future please try to find a more appropriate place
for it.
p.s. -- I found this out the hard way years ago on VMS with
a password encryption file that was being used for RIT.
Every now and then parts of the file were getting scrambled,
and I finally discovered that this translation was causing it.
Author: "G nter Nagler" <gnagler@ihm.tu-graz.ac.at>
Date: 13 Mar 1995 12:34:18 GMT Raw View
> This question is about the writing of individual characters to a file.
> I am trying to create a binary file, and each time I write the character 0A
> (Hex) it is translated into the pair 0D0A (Hex). I have tried putc, fputc,
> fwrite, and putw. There must be a way to write 0A without it being
> translated into a 0D0A pair. I am using DOS with two different Borland
> compilers. Borland C++ 4.5 and Borland C++ 2.0. They both act the same way.
out = fopen(outfile, "wb");
opens the file for binary write
G nter Nagler
Author: sehlke@cts.com (Steve Ehlke)
Date: Fri, 10 Mar 1995 19:25:51 GMT Raw View
This question is about the writing of individual characters to a file.
I am trying to create a binary file, and each time I write the character 0A
(Hex) it is translated into the pair 0D0A (Hex). I have tried putc, fputc,
fwrite, and putw. There must be a way to write 0A without it being
translated into a 0D0A pair. I am using DOS with two different Borland
compilers. Borland C++ 4.5 and Borland C++ 2.0. They both act the same way.
Here's a code sample for you.
int fh; /* file handles */
FILE *out; /* file input & output streams */
div_t x;
union bth {
unsigned int counter;
char outc[2];
} both;
both.counter = 0;
out = fopen(outfile, "w");
while(both.counter <= (unsigned int)32767)
{
x = div(both.counter,2);
if (x.rem == 0)
{
/* fwrite(&(both.outc[1]),1,1,out);*/ /* put char to output file */
/* fwrite(&(both.outc[0]),1,1,out);*/ /* put char to output file */
/* putw(both.counter, out); */
fprintf(out, "%c", both.outc[1]);
fprintf(out, "%c", both.outc[0]);
both.counter++;
}
else both.counter++;
} /* End While */
putc('\r',out); /* a final carriage return to clear */
/* the printer buffer */
if (fcloseall() == EOF) return FILE_CLOSE_ERROR;
Author: Edward Diener <70304.2632@CompuServe.COM>
Date: 11 Mar 1995 19:31:56 GMT Raw View
You need to change the way you open a file to binary. The default
is text which changes all 0x0a ( new-line ) to a 0x0a-0x0d pair.
Try "out = fopen(outfile,"wb")" . That should do the trick.