C语言 如何在 C 中发送 ctrl+z
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16132971/
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 send ctrl+z in C
提问by omerjerk
I'm working with Arduino.
我正在与 Arduino 合作。
I want to send Ctrl+zafter a string in C. I tried truncating ^Zbut that didn't work. So how to do that ?
我想在 C 中的一个字符串之后发送Ctrl+ z。我尝试过截断,^Z但没有奏效。那么怎么做呢?
回答by Jonathan Leffler
Ctrl+Z= 26 = '\032'= '\x1A'. Either of the backslash escape sequences can be written in a string literal (but be careful with the hex escape as if it is followed by a digit or A-F or a-f, that will also be counted as part of the hex escape, which is not what you want).
Ctrl+ Z= 26 = '\032'= '\x1A'。任何一个反斜杠转义序列都可以写在字符串文字中(但要小心十六进制转义,好像它后面跟着一个数字或 AF 或 af,这也将被视为十六进制转义的一部分,这不是什么你要)。
However, if you are simulating terminal input on a Windows machine (so you want the character to be treated as an EOF indication), you need to think again. That isn't how it works.
但是,如果您在 Windows 机器上模拟终端输入(因此您希望将字符视为 EOF 指示),则需要重新考虑。这不是它的工作原理。
It may or may not do what you want with Arduino, either; in part, it depends on what you think it is going to do. It also depends on whether the input string will be treated as if it came from a terminal.
它可能会也可能不会用 Arduino 做你想做的事情;在某种程度上,这取决于您认为它将要做什么。它还取决于是否将输入字符串视为来自终端。
回答by Neil McGill
I hacked this up as I needed similar
我修改了这个,因为我需要类似的
#include <stdio.h>
#define CTRL(x) (#x[0]-'a'+1)
int main (void)
{
printf("hello");
printf("%c", CTRL(n));
printf("%c", CTRL(z));
}
hope it helps 8)
希望有帮助 8)
回答by paxdiablo
I assume by "truncating" you actually meant appending.
我认为“截断”实际上是指追加。
In ASCII, CTRL+zis code point 26 so you can simply append that as a character, something like:
在 ASCII 中,CTRL+z是代码点 26,因此您可以简单地将其附加为一个字符,例如:
#define CTRL_Z 26
char buffer[100];
sprintf (buffer, "This is my message%c", CTRL_Z);
The sprintfmethod is only oneof the ways of doing this but they all basically depend on you putting a single byte at the end with the value 26.
该sprintf方法只是执行此操作的一种方法,但它们基本上都取决于您在末尾放置一个值为 26 的字节。

