Why is an extra \ r entered in 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 is extra \rintroduced in windows (doesn't happen on linux)?

+3
source share
3 answers

This is a Windows feature dating back to the early days of MS-DOS. On these systems, the convention is that the line separator is a pair of " \r\n" characters . Of course on Linux / Unix / Solaris / etc. The line separator is a single character " \n"

There are various utilities such as Linux dos2unixand unix2dosthat do nothing but this conversion. Almost every file transfer program has a means to combat it. See mode command kermit.

MSDOS/windows C fopen() ( ): b t, . t ext \r\n \n \n \r\n . A b inary conversion .

FILE *f1 = fopen ("somefile.txt", "rt");  /* open in text conversion mode */
FILE *f2 = fopen ("anotherfile.bin", "rb");  /* open without text conversion */
+7

. windows :\r\n, int constract, linux/unix -\n

+1

Windows, Unix Mac - .

Windows uses \ r \ n, Unix / Linux \ n, Mac \ r. When you start chatting with text files on multiple platforms, it becomes a real mess. This is why any serious text editor has the ability to switch between them and why Linux has utilities like dos2unix. Try uploading a Unix style text file (only with \ n) to Notepad. You will notice all this on one line.

+1
source

All Articles