C++ '\r' 转义序列的用途是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7372918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:50:49  来源:igfitidea点击:

What's the Use of '\r' escape sequence?

c++cescapingcarriage-return

提问by Ant's

I have C code like this:

我有这样的 C 代码:

#include<stdio.h>
int main()
{
    printf("Hey this is my first hello world \r");
    return 0;
}

I have used the \rescape sequence as an experiment. When I run the code I get the output as:

我已经将\r转义序列用作实验。当我运行代码时,我得到的输出为:

o world

Why is that, and what is the use of \rexactly?

为什么会这样,究竟有什么用\r

If I run the same code in an online compiler I get the output as:

如果我在在线编译器中运行相同的代码,我会得到如下输出:

Hey this is my first hello world

Why did the online compiler produce different output, ignoring the \r?

为什么在线编译器会产生不同的输出,而忽略了\r?

回答by Arnaud Le Blanc

\ris a carriage returncharacter; it tells your terminal emulator to move the cursor at the start of the line.

\r回车符;它告诉您的终端模拟器在行首移动光标。

The cursoris the position where the next characters will be rendered.

光标是下一个字符将被呈现的位置。

So, printing a \rallows to override the current line of the terminal emulator.

因此,打印 a\r允许覆盖终端模拟器的当前行。

Tom Zychfigured why the output of your program is o worldwhile the \ris at the end of the line and you don't print anything after that:

Tom Zych想出了为什么你的程序的输出是o world\ris 在行尾而你之后不打印任何东西:

When your program exits, the shell prints the command prompt. The terminal renders it where you left the cursor. Your program leaves the cursor at the start of the line, so the command prompt partly overrides the line you printed. This explains why you seen your command prompt followed by o world.

当您的程序退出时,shell 会打印命令提示符。终端将其呈现在您离开光标的位置。您的程序将光标留在行首,因此命令提示符会部分覆盖您打印的行。这解释了为什么您看到命令提示符后跟o world.

The online compileryou mention just prints the raw output to the browser. The browser ignores control characters, so the \rhas no effect.

您提到的在线编译器只是将原始输出打印到浏览器。浏览器忽略控制字符,因此\r无效。

See https://en.wikipedia.org/wiki/Carriage_return

https://en.wikipedia.org/wiki/Carriage_return

Here is a usage example of \r:

这是一个使用示例\r

#include <stdio.h>
#include <unistd.h>

int main()
{
        char chars[] = {'-', '\', '|', '/'};
        unsigned int i;

        for (i = 0; ; ++i) {
                printf("%c\r", chars[i % sizeof(chars)]);
                fflush(stdout);
                usleep(200000);
        }

        return 0;
}

It repeatedly prints the characters -\|/at the same position to give the illusion of a rotating |in the terminal.

-\|/在同一位置重复打印字符,|以在终端中产生旋转的错觉。

回答by Tom Zych

The program is printing "Hey this is my first hello world ", then it is moving the cursor back to the beginning of the line. How this will look on the screen depends on your environment. It appears the beginning of the string is being overwritten by something, perhaps your command line prompt.

程序正在打印"Hey this is my first hello world ",然后将光标移回行首。这将如何在屏幕上显示取决于您的环境。字符串的开头似乎被某些东西覆盖,也许是您的命令行提示符。

回答by Xavier Holt

The '\r' stands for "Carriage Return" - it's a holdover from the days of typewriters and really old printers. The best example is in Windows and other DOSsy OSes, where a newline is given as "\r\n". These are the instructions sent to an old printer to start a new line: first move the print head back to the beginning, then go down one.

'\r' 代表“回车”——它是打字机和真正老式打印机时代的产物。最好的例子是在 Windows 和其他 DOSsy 操作系统中,其中换行符为“\r\n”。这些是发送到旧打印机以开始新行的指令:首先将打印头移回开头,然后向下移动一个。

Different OSes will use other newline sequences. Linux and OSX just use '\n'. Older Mac OSes just use '\r'. Wikipediahas a more complete list, but those are the important ones.

不同的操作系统将使用其他换行序列。Linux 和 OSX 只使用 '\n'。较旧的 Mac 操作系统只使用 '\r'。 维基百科有一个更完整的列表,但这些是重要的。

Hope this helps!

希望这可以帮助!

PS: As for why you get that weird output... Perhaps the console is moving the "cursor" back to the beginning of the line, and then overwriting the first bit with spaces or summat.

PS:至于为什么你会得到那个奇怪的输出......也许控制台正在将“光标”移回行首,然后用空格或 summat 覆盖第一位。

回答by Nornagest

\rmove the cursor to the begin of the line.

\r将光标移动到行首。

Line breaks are managed differently on different systems. Some only use \n(line feed, e.g. Unix), some use (\re.g. MacOS before OS X afaik) and some use \r\n(e.g. Windows afaik).

换行符在不同系统上的管理方式不同。有些只使用\n(换行,例如 Unix),有些使用(\r例如 OS X afaik 之前的 MacOS)和一些使用\r\n(例如 Windows afaik)。

回答by wchargin

As amaud576875 said, the \rescape sequence signifies a carriage-return, similar to pressing the Enter key. However, I'm not sure how you get "o world"; you should (and I do) get "my first hello world" and then a new line. Depending on what operating system you're using (I'm using Mac) you might want to use a \ninstead of a \r.

正如 amaud576875 所说,\r转义序列表示回车,类似于按 Enter 键。但是,我不确定您是如何获得“o world”的;你应该(我也这样做)得到“我的第一个你好世界”,然后是一个新行。根据你使用(我使用的是Mac)是什么操作系统,你可能想使用\n的替代\r

回答by Toby Speight

To answer the part of your question,

要回答你的问题的一部分,

what is the use of \r?

有什么用\r

Many Internet protocols, such as FTP, HTTP and SMTP, are specified in terms of lines delimited by carriage return and newline. So, for example, when sending an email, you might have code such as:

许多 Internet 协议,例如 FTP、HTTP 和 SMTP,都是根据由回车和换行符分隔的行来指定的。因此,例如,在发送电子邮件时,您可能有如下代码:

fprintf(socket, "RCPT TO: %s\r\n", recipients);

Or, when a FTP server replies with a permission-denied error:

或者,当 FTP 服务器回复权限被拒绝错误时:

fprintf(client, "550 Permission denied\r\n");

回答by Steve Wellens

This is from antiquated technology: The old fashion typewriter style of printer. There was a roller (platen) that advanced the paper and a print head that hammered a metal key against an ink fabric.

这是来自过时的技术:打印机的旧时尚打字机风格。有一个滚筒(压板)推动纸张前进,还有一个打印头将金属键锤击在油墨织物上。

\r Return the print head to the left side.

\r 将打印头放回左侧。

\n Advance the platen one line.

\n 将压板推进一行。

If the \n was not issued, you would type over what was on a line (used mostly for underlining text).

如果未发出 \n,您将输入一行中的内容(主要用于为文本加下划线)。

回答by EvilTeach

It is quite useful, when you are running on the unix platform, and need to create a text file which will be opened on the dos platform.

当你在unix平台上运行,并且需要创建一个将在dos平台上打开的文本文件时,它非常有用。

Unix uses '\n' as its line terminator, and dos uses '\r\n' as its line terminator, so you can use it to create a dos text file.

Unix 使用'\n' 作为它的行终止符,而dos 使用'\r\n' 作为它的行终止符,所以你可以用它来创建一个dos 文本文件。