about: fstream << "\r\n" | | |
hi, in win2000 and VC6, I operate a txt file. When I write a line data into
file and add "\r\n" at the end of line, I find:
fstream << "\r\n" write 3 chars : 0D 0D 0A
fstream << "\n" write 2 chars : 0D 0A
why? | | | | re: about: fstream << "\r\n"
On Wed, 14 Jul 2004 11:40:01 +0800, Rate Spring <spring_pb@21cn.com> wrote:
[color=blue]
> hi, in win2000 and VC6, I operate a txt file. When I write a line data
> into
> file and add "\r\n" at the end of line, I find:
>
> fstream << "\r\n" write 3 chars : 0D 0D 0A
> fstream << "\n" write 2 chars : 0D 0A
>
> why?
>[/color]
Because you've opened the file in text mode. '\n' is the newline character
and \r\n is the end of line sequence on Windows operating systems. You
will also find that the reverse translation happens on input, \r\n will
read as a single character '\n'. So when you read back you'll get the
exact same characters you output.
Normnally this is convenient, it lets you port programs between Unix and
Windows say. But if you don't want it to happen then open the file in
binary mode.
fstream file("something.dat", ios_base::out|ios_base::binary);
john | | | | re: about: fstream << "\r\n"
"Rate Spring" <spring_pb@21cn.com> wrote in message
news:cd29up$2kgm$1@mail.cn99.com...[color=blue]
> hi, in win2000 and VC6, I operate a txt file. When I write a line data[/color]
into[color=blue]
> file and add "\r\n" at the end of line, I find:
>
> fstream << "\r\n" write 3 chars : 0D 0D 0A
> fstream << "\n" write 2 chars : 0D 0A
>[/color]
John has given you the answer. To see it more clearly feed a Windows files
to some Unix compiler. Say compile with g++ on windows with cygwin and feed
it a Windows file. It will misinterpret the file then.
-Sharad | | | | re: about: fstream << "\r\n"
On Wed, 14 Jul 2004 11:48:35 +0530, Sharad Kala
<no__spam.sharadk_ind@yahoo.com> wrote:
[color=blue]
>
> "Rate Spring" <spring_pb@21cn.com> wrote in message
> news:cd29up$2kgm$1@mail.cn99.com...[color=green]
>> hi, in win2000 and VC6, I operate a txt file. When I write a line data[/color]
> into[color=green]
>> file and add "\r\n" at the end of line, I find:
>>
>> fstream << "\r\n" write 3 chars : 0D 0D 0A
>> fstream << "\n" write 2 chars : 0D 0A
>>[/color]
>
> John has given you the answer. To see it more clearly feed a Windows
> files
> to some Unix compiler. Say compile with g++ on windows with cygwin and
> feed
> it a Windows file. It will misinterpret the file then.
>[/color]
Yes, I should have said it lets you port *code* between Windows and Unix,
it's of no help porting the file itself.
john | | | | re: about: fstream << "\r\n"
"Rate Spring" <spring_pb@21cn.com> wrote in message news:cd29up$2kgm$1@mail.cn99.com...[color=blue]
> hi, in win2000 and VC6, I operate a txt file. When I write a line data into
> file and add "\r\n" at the end of line, I find:
>
> fstream << "\r\n" write 3 chars : 0D 0D 0A
> fstream << "\n" write 2 chars : 0D 0A
>
> why?[/color]
In DOS and Windows, a newline is two bytes:
carriage-return (0x0D) followed by line-feed (0x0A).
'\n' is CR-LF, or 0x0D0A.
'\r' is just CR, or 0x0D.
Hence "\r\n" is 0x0D0D0A.
(In Unix or linux, a newline is just one byte,
so none of the above applies.)
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant | | | | re: about: fstream << "\r\n"
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message news:40f4e7da$1_2@127.0.0.1...
[color=blue]
> In DOS and Windows, a newline is two bytes:
> carriage-return (0x0D) followed by line-feed (0x0A).
>
> '\n' is CR-LF, or 0x0D0A.
>[/color]
Completely and totally incorrect. '\n' is a single character on every compliant
C and C++ compiler in the world. It is the C++ streams and C FILE streams
that map the single '\n' character into something implementation specific (which on
most non-UNIX platforms is 0D 0A. | | | | re: about: fstream << "\r\n"
"Ron Natalie" <ron@sensor.com> wrote in reply to
a message of mine in this group a few days ago:
[color=blue]
> "Robbie Hatley" wrote:[color=green]
> > In DOS and Windows, a newline is two bytes:
> > carriage-return (0x0D) followed by line-feed (0x0A).
> >
> > '\n' is CR-LF, or 0x0D0A.
> >[/color]
> Completely and totally incorrect.[/color]
Perhaps I should clarify. Replace "is" with "maps to":
'\n' maps to CR-LF, or 0x0D0A
[color=blue]
> '\n' is a single character on every compliant
> C and C++ compiler in the world.[/color]
I wrote "In DOS and Windows", not "In compliant C and C++
compilers".
Newline is a single character in registers and RAM during
program execution, yes, but must be converted to 0x0D0A
when talking to MS-DOS or MS-Windows.
[color=blue]
> It is the C++ streams and C FILE streams that map the
> single '\n' character into something implementation
> specific (which on most non-UNIX platforms is 0D 0A).[/color]
I don't know about "most non-UNIX platforms", but yes,
C and C++ implimentations on MS-DOS and MS-Windows do
have to convert '\n' to 0x0D0A (outbound) or 0x0D0A to
'\n' (inbound) when talking to the operating system.
This is what I was trying to say in my original post.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|