How to pass in a null character in a command line argument in C?

*爱你&永不变心* 提交于 2019-12-10 01:10:06

问题


I would still like to know how to pass in a null character as a command line argument, maybe so that a single string can be passed in as an argument in the form:

"to\0be\0or\0not\0to\0be\0"

And then parse it. However the program would treat this string as:

"to\\0be\\0or\\0not\\0to\\0be\\0"

How can I work around this? Is there any way?


回答1:


You cannot.

The C program receives arguments as zero-terminated strings. Such a string cannot contain a null character, by definition.

If you want to pass a null character, then you must somewhat encode it with some syntax, and your C program must then decode it by interpreting that syntax.




回答2:


C strings are null-terminated, so passing strings containing NUL characters is not possible in C. :-P

Now, if you just wanted a way to convert \0 (in the user input, i.e., "\\0" as a C string) into actual NUL characters, that's another matter. In that case, your program just needs a parser to treat \0 as separators.



来源:https://stackoverflow.com/questions/2283335/how-to-pass-in-a-null-character-in-a-command-line-argument-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!