How to pass in a null character in a command line argument in C?
I would still开发者_如何学Python 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?
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.
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.
精彩评论