免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: nine8
打印 上一主题 下一主题

[悬赏] [请教] C程序, 反斜杠 \ 作为命令行参数,会被当做转义符还是字符反斜杠 [复制链接]

论坛徽章:
0
6 [报告]
发表于 2010-06-22 10:03 |只看该作者
lz一点发帖,都是球迷啊。。。

论坛徽章:
0
5 [报告]
发表于 2010-06-22 10:03 |只看该作者
看球的吧。。。

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2010-06-22 08:54 |只看该作者
LS在中国么?2:30还在回帖{:3_186:}

论坛徽章:
0
3 [报告]
发表于 2010-06-22 02:30 |只看该作者
1. 这个与C无关,命令行的语法这么规定的,方便如文件名带空格作为参数等特殊情况的表示。
2. argv收到的已经是命令行shell处理并且传入的了,所以反斜杠转义已经完成,得到的是转义之后的结果。
3. 从命令行参数中获得在C字符串表达的\,直接在命令行用\\就可以了。
4. 你可以预处理一遍啊,比如实现一个函数pre_translate( argv[1], out_arg, &out_len ),输出到out_arg,看到\就转成\\;还有一个办法是命令行传入时,直接写成\\\\。

论坛徽章:
0
2 [报告]
发表于 2010-06-22 01:05 |只看该作者
本帖最后由 没本 于 2010-06-23 01:58 编辑

你显然还没把sh/bash的文法和C文法区分清楚。你的命令行是sh/bash文法,argv拿到的就是按C文法处理了。在sh/bash文法里面,\a是无效的,所以\被忽略了。参见:
http://www.gnu.org/software/bash ... ml#Escape-Character

  1. 3.1.2.1 Escape Character

  2. A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).
复制代码
另外bash还提供了一个语法(注意要用$'带反斜杠字符串'):
http://www.gnu.org/software/bash ... #ANSI_002dC-Quoting

  1. 3.1.2.4 ANSI-C Quoting

  2. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

  3. \a
  4. alert (bell)
  5. \b
  6. backspace
  7. \e
  8. \E
  9. an escape character (not ANSI C)
  10. \f
  11. form feed
  12. \n
  13. newline
  14. \r
  15. carriage return
  16. \t
  17. horizontal tab
  18. \v
  19. vertical tab
  20. \\
  21. backslash
  22. \'
  23. single quote
  24. \"
  25. double quote
  26. \nnn
  27. the eight-bit character whose value is the octal value nnn (one to three digits)
  28. \xHH
  29. the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
  30. \cx
  31. a control-x character
  32. The expanded result is single-quoted, as if the dollar sign had not been present.
复制代码
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP