Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why additional \r is injected in windows?

Tags:

c++

windows

#include <iostream>
using namespace std;
int main( int argc, char *argv[])
{
    cout << "Hello\nWorld";
}

D:\test>hw |od -c
0000000   H   e   l   l   o  \r  \n   W   o   r   l   d
0000014

Why additional \r is injected in windows(doesn't happen on linux) ?

like image 575
DriverBoy Avatar asked Dec 09 '25 20:12

DriverBoy


1 Answers

It is a feature of Windows going back to the first days of MS-DOS. In those systems, the convention is that a line delimiter is the character pair "\r\n". Of course, in Linux/Unix/Solaris/etc., the line delimiter is the single character "\n"

There are various utilities, such as Linux's dos2unix and unix2dos which do nothing but this transformation. Virtually every file transfer program has a means of dealing with it too. See kermit's mode command.

The convention affected the MSDOS/windows C runtime library function fopen() (among others): the second parameter can have a b or t to explicitly set the line delimiter conversion. A text conversion transforms \r\n to \n on input and \n to \r\n on output. A binary conversion does no such transformation.

FILE *f1 = fopen ("somefile.txt", "rt");  /* open in text conversion mode */
FILE *f2 = fopen ("anotherfile.bin", "rb");  /* open without text conversion */
like image 195
wallyk Avatar answered Dec 11 '25 10:12

wallyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!