错误:十六进制逃逸序列超出范围,写功能是从3个区域读取12个字节
我的代码引起了问题:
if (write(STDOUT_FILENO, "\x1b999C\x1b999B", 12) != 12)
return -1;
在这里,write()来自< unistd.h>
。
因此,在编译时提供了两个警告。 “ \ x1b999c \ x1b9999b”
不超出范围,'写''读数12字节从大小3的区域。
我基本上试图做的是将光标移至底部,终端窗口的右角。为什么那里有这些警告?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
八分之一仅使用一到三个(八进制)数字 -  
“ \ 033999c”
是5个字符加上一个null字节。十六进制逃脱使用的数字数量如
\ x
序列。因此,“ \ x1b999c”
包含一个单个十六进制字符常数。参见§6.4.4.4.4用于规格。
至少有两个可能的修复程序。一种是编码
\ x1b
为\ 033
(或\ 33
- 它们是相同的,尽管如果之后的文本\ x1b
是777a
而不是999c
,那么三位数的八位ovene是必需的。串联:
这是因为在 - 第5阶段转换逃脱序列,第6阶段结合了相邻的字符串文字
。这是10个非挂钩字符 - 如果您打算编写字节0x01,然后是5个字符(两次),那么12个字符更有意义,并且您需要更改字符串中断的地方
。 > 11 :
删除序列,GCC投资:
我使用
Werror
选项进行编译,否则,只有警告没有-werror
并运行,它会产生答案3,而不是11。Octal escapes use one to three (octal) digits only —
"\033999C"
is 5 characters plus a null byte.Hex escapes use as many hex digits as follow the
\x
sequence. Therefore,"\x1b999C"
contains a single hex character constant.See §6.4.4.4 Character constants in the C11 standard for the specification.
There are at least two possible fixes. One is to encode
\x1B
as\033
(or\33
— they're the same, though if the text after the\x1B
was777A
instead of999C
, then the triple-digit octal escape would be necessary.A second is to split the string and use string concatenation:
This works because of what is specified in §5.1.1.2 Translation phases — where phase 5 converts escape sequences and phase 6 combines adjacent string literals.
I observe that there are 10 non-null characters in this — if you intended to write the byte 0x01 followed by 5 characters (twice), then 12 makes more sense, and you need to alter where the strings break, of course.
This test code prints
11
:Remove the
" "
sequences, and GCC complains:I compile with the
-Werror
option; otherwise, it would only be a warning. If compiled without-Werror
and run, it produces the answer 3, not 11.