简体   繁体   English

C ++将char *的ASCII字符转换为Unix文件名

[英]c++ convert a char* of ascii characters to a unix filename

I have a char* which only contains ASCII characters (decimal: 32-126). 我有一个char *,仅包含ASCII字符(十进制:32-126)。 I'm searching for a c++ function which escapes (add a backslash before the character) characters that have special meanings in the unix filesystem like '/' or '.'. 我正在寻找一个c ++函数,该函数在unix文件系统中转义具有特殊含义的字符(在字符前添加反斜杠),例如“ /”或“。”。 I want to open the file with fopen later. 我想稍后使用fopen打开文件。

I'm not sure, if manually replacing would be a good option. 我不确定,如果手动更换将是一个不错的选择。 I don't know all characters with special meanings. 我不知道所有具有特殊含义的字符。 I also don't know if '?' 我也不知道是否? or '*' would work with fopen. 或'*'将与fopen一起使用。

Actually Unix (or more specific the SuS) disallows only the byte values '/' and '\\0' in file names. 实际上,Unix(或更具体而言,SuS)不允许文件名中的字节值“ /”和“ \\ 0”。 Everything else actually is fair game. 其他一切实际上都是公平的游戏。 The exact (in the sense that they're immediately following and followed by a '/') strings "." 确切的字符串(在它们后面紧跟着一个“ /”)。 and ".." are reserved to relative path access, but they are very well valid in a Unix path. 和“ ..”保留给相对路径访问,但是它们在Unix路径中非常有效。

And of course any number and sequence of '.' 当然还有任何数量和顺序的“。” is perfectly allowed in a Unix filename, as long as another character other than '/' or '\\0' is part of the filename. 只要在文件名中包含'/'或'\\ 0'以外的其他字符,在Unix文件名中完全允许使用。 Yes, newline, any control character, they're all perfectly valid Unix filenames. 是的,换行符,任何控制字符,它们都是完全有效的Unix文件名。

Of course the file system you're using may have a different idea about what's permissible, but you were just asking about Unix. 当然,您使用的文件系统对允许的内容可能会有不同的想法,但是您只是在询问Unix。

Update: 更新:

Oh and it should be noted, that Unix doesn't specify dome "parse" method for filenames. 哦,应该指出的是,Unix没有为文件名指定穹顶“ parse”方法。 Which essentially means, a filename is treated as a binary blob key into a key→value database. 从本质上讲,文件名被视为键→值数据库中的二进制Blob键。 It also means, that there's no such thing as "escaping" for Unix filenames. 这也意味着,对于Unix文件名,没有“转义”之类的东西。

POSIX filenames don't have a concept of escape characters. POSIX文件名没有转义字符的概念。 There is no way to have a slash as an element of a filename (when the system renders filenames using Unicode you may be able to create a filename which looks as if it contains a slash, though). 没有办法将斜杠作为文件名的元素(但是,当系统使用Unicode呈现文件名时,您可能能够创建看起来好像包含斜杠的文件名)。 I think all other printable characters are just fine although using special characters like * and ? 我认为所有其他可打印字符都很好,尽管使用了*?类的特殊字符? in filename will probably cause problems when people try use them from a shell. 当人们尝试从shell中使用它们时,文件名中的可能会引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM