C语言 如何转到 C 代码中的上一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4895541/
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
How to go to the previous line in a C code
提问by Renjith G
If for the following code:
如果对于以下代码:
printf("HEllo\n"); // do not change this line.
printf("\b\bworld");
I need an output: Helloworld (In a single line). But this does not work fine. Could anyone please explain the reason? And other escape sequence if any.
我需要一个输出:Helloworld(在一行中)。但这行不通。任何人都可以请解释原因吗?以及其他转义序列(如果有)。
回答by templatetypedef
There is no platform-independent control character to move back up a line. This harkens back to the days of line printers, where printfactually would print a line of text onto a sheet of paper and there was no way of retracting the paper to overwrite what had already been printed.
没有独立于平台的控制字符向上移动一行。这让人回想起行式打印机的时代,printf实际上将一行文本打印到一张纸上,并且无法收回纸张以覆盖已经打印的内容。
That said, there are libraries like ncurses that let you move the cursor around the console. They're just not part of the standard libraries.
也就是说,有像 ncurses 这样的库可以让你在控制台周围移动光标。它们只是不是标准库的一部分。
回答by Cody Gray
How about simply:
简单地说:
printf("Helloworld");
\nis an escape sequence for a new line. Since you want everything to appear on the sameline, there's no reason to specify \n.
\n是换行的转义序列。由于您希望所有内容都出现在同一行上,因此没有理由指定\n.
The problem is you can't reliably move back upa line (using \b) after you've printed a new line. But if you require that there be two lines of code in your source, you can simply omit both escape sequences:
问题是你不能可靠地移动备份线路(使用\b)你已经印制了新的行之后。但是,如果您要求源代码中有两行代码,则可以简单地省略两个转义序列:
printf("HEllo");
printf("world");
If you're writing a Win32 console application, you can take advantage of the Console Screen Buffer API. The following code will move 1 line up:
如果您正在编写 Win32 控制台应用程序,则可以利用Console Screen Buffer API。以下代码将向上移动 1 行:
printf("HEllo\n");
CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1; // move up one line
coninfo.dwCursorPosition.X += 5; // move to the right the length of the word
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);
printf("world");
Output:
输出:
HElloworld
你好,世界
回答by Vladimir Ivanov
Remove "\n" from your first printf. It moves the cursor to a new line.
从您的第一个 printf 中删除“\n”。它将光标移动到新行。
Hereis the list of escape sequences.
这是转义序列的列表。
If you can't remove "\n", then you can do make a copy of a substring without these charaters. See the following example:
如果您不能删除“\n”,那么您可以制作一个没有这些字符的子字符串的副本。请参阅以下示例:
const char* from = "12345678";
char *to = (char*) malloc(6);
strncpy(to, from+2, 5);
All you need is to determine the index of "\n" characters.
您只需要确定“\n”字符的索引即可。
回答by tdammers
The backspace character, when sent to a stream (such as through the printf()family of functions), does not seek backward in the file, it is sent as-is. If you run your example, the backspace character will be output as "garbage".
退格字符在发送到流时(例如通过printf()函数系列),不会在文件中向后查找,而是按原样发送。如果您运行您的示例,退格字符将输出为“垃圾”。
If you don't want a new line, don't post a newline character.
如果您不想换行,请不要发布换行符。
回答by Liviu M.
It is because '\b' is a terminal escape code... (sans the 'escape' of course ;-)
这是因为 '\b' 是一个终端转义码......(当然没有 'escape' ;-)
so it only modifies what you see on the terminal. That is, the terminal adjusts its display to respond to the backspace code, even though it received everything prior to it. A file also receives everything, including the backspace code, but it is not a tty device so the file's content is not modified; it keeps everything you send to it.
所以它只会修改您在终端上看到的内容。也就是说,终端调整其显示以响应退格码,即使它收到了之前的所有内容。文件还接收所有内容,包括退格代码,但它不是 tty 设备,因此文件的内容不会被修改;它保留您发送给它的所有内容。
If printing an extra blank is a problem, then you should code some extra logic to print the trailing blank on every output except the last.
如果打印额外的空白是一个问题,那么您应该编写一些额外的逻辑来在除最后一个输出之外的每个输出上打印尾随空白。
ReferenceThis is in reference to files but maybe same idea applies. You might want to check out this link there is a very detailed explanation that most probably answers your question.
参考这是参考文件,但也许同样的想法适用。您可能想查看此链接,那里有非常详细的解释,很可能会回答您的问题。
回答by Project Zero
You can do it actually in a platform-independent* way, assuming that you can somehow calculate the x offset of the previous line.
您实际上可以以独立于平台的* 方式进行,假设您可以以某种方式计算前一行的 x 偏移量。
int x = printf("Hello, World!\n");
gotoxy(x-1-1,gety()-1); // One for the length-offset difference and the other to skip \n
printf("\b \b");
You can avoid using that variable by directly replacing it with x.
您可以通过直接将其替换为 x 来避免使用该变量。
Note: printf() returns an int(the length of the passed String (of characters)). Use it wisely :)
注意:printf() 返回一个int(传递的字符串(字符)的长度)。明智地使用它:)
回答by ehudokai
In a console, you can't go up a line. You can clear the screen and redraw it (which simulate going up a line.) Or you can rewrite on the same line. \r will take you to the beginning of the line you just printed.
在控制台中,你不能上楼。您可以清除屏幕并重新绘制它(模拟向上一行。)或者您可以在同一行上重写。\r 将带您到刚刚打印的行的开头。

