473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?
Jul 22 '05 #1
6 6613
On Wed, 14 Jul 2004 11:40:01 +0800, Rate Spring <sp*******@21cn.com> wrote:
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?


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
Jul 22 '05 #2

"Rate Spring" <sp*******@21cn.com> wrote in message
news:cd***********@mail.cn99.com...
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


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

Jul 22 '05 #3
On Wed, 14 Jul 2004 11:48:35 +0530, Sharad Kala
<no******************@yahoo.com> wrote:

"Rate Spring" <sp*******@21cn.com> wrote in message
news:cd***********@mail.cn99.com...
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


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.


Yes, I should have said it lets you port *code* between Windows and Unix,
it's of no help porting the file itself.

john
Jul 22 '05 #4

"Rate Spring" <sp*******@21cn.com> wrote in message news:cd***********@mail.cn99.com...
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?


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
Jul 22 '05 #5

"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message news:40**********@127.0.0.1...
In DOS and Windows, a newline is two bytes:
carriage-return (0x0D) followed by line-feed (0x0A).

'\n' is CR-LF, or 0x0D0A.

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.

Jul 22 '05 #6
"Ron Natalie" <ro*@sensor.com> wrote in reply to
a message of mine in this group a few days ago:
"Robbie Hatley" wrote:
In DOS and Windows, a newline is two bytes:
carriage-return (0x0D) followed by line-feed (0x0A).

'\n' is CR-LF, or 0x0D0A.
Completely and totally incorrect.


Perhaps I should clarify. Replace "is" with "maps to":

'\n' maps to CR-LF, or 0x0D0A
'\n' is a single character on every compliant
C and C++ compiler in the world.
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.
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).


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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BJörn Lindqvist | last post by:
I think it would be cool if you could refer to instance variables without prefixing with "self." I know noone else thinks like me so Python will never be changed, but maybe you can already do it...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
1
by: Bernd Hohmann | last post by:
I have a question about using "*" in (sub)queries. This example query counts the number of records for a certain customer starting with record "80172" and stops after 10 rows. SELECT...
7
by: James Johnson | last post by:
Are there structs in JavaScript? If not, what's the closest thing, or do I just use parallel arrays? I'm populating a JavaScript array from ColdFusion query, but I don't think I can do this: ...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.