【发布时间】:2016-01-27 19:51:16
【问题描述】:
我的代码中有一个使用 awk 的 system() 命令。我不知道如何解决\x00 十六进制值的问题。显然他们需要以不同的方式终止,但这超出了我所知道的范围。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char command[128];
snprintf(command, sizeof(command), "awk '{ gsub (/\xAB\x00\x00\xBC/,\"\xBC\x00\x00\xAB\") ; print }' %s", argv[1]);
system(command);
}
警告/错误:
> test.c:8:56: warning: format string contains '\0' within the string body [-Wformat]
> snprintf(command, sizeof(command), "awk '{ gsub (/\xAB\x00\xBC/,\"\xBC\x00\x00\xAB\") ; print }' %s", argv[1]);
> /usr/include/secure/_stdio.h:57:62: note: expanded from macro 'snprintf'
> __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)
^
1 warning generated.
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
抱歉,如果之前有人问过这个问题,我在搜索如何解决这个问题时找不到任何相关信息,谢谢...
【问题讨论】:
-
它可以从命令行运行,尽管我不需要像
C中那样转义" -
用
\\x替换\x的每个实例。 -
我在终端
awk '{ gsub (/\xAB\x00\x00\xBC/,"\xBC\x00\x00\xAB") ; print }' file这样使用 -
C 字符串中不能有空字符。 \x00 表示空字符,与 \0 相同。但是,如果您对反斜杠进行转义,则可以将 \x00 放入您的 C 字符串中(如反斜杠、ex、零、零,而不是空字符)。