c ++将ascii字符的char *转换为unix文件名

时间:2021-10-03 23:54:28

I have a char* which only contains ASCII characters (decimal: 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 '.'. I want to open the file with fopen later.

我有一个char *只包含ASCII字符(十进制:32-126)。我正在寻找一个c ++函数,它在unix文件系统中具有特殊含义的转义(在字符前加一个反斜杠),如'/'或'。'。我想稍后用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一起使用。

2 个解决方案

#1


4  

Actually Unix (or more specific the SuS) disallows only the byte values '/' and '\0' in file names. 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(或更具体的SuS)不允许文件名中的字节值'/'和'\ 0'。其他一切实际上都是公平的游戏。确切的(从某种意义上说它们紧跟在后面跟着'/')字符串“。”并且“..”保留给相对路径访问,但它们在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. Yes, newline, any control character, they're all perfectly valid Unix filenames.

当然还有任何数量和顺序的'。'完全允许在Unix文件名中,只要除“/”或“\ 0”之外的其他字符是文件名的一部分。是的,换行符,任何控制字符,它们都是完全有效的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. Which essentially means, a filename is treated as a binary blob key into a key→value database. It also means, that there's no such thing as "escaping" for Unix filenames.

哦,应该注意的是,Unix没有为文件名指定圆顶“解析”方法。这实际上意味着,文件名被视为一个二进制blob键,进入一个键→值数据库。这也意味着,对于Unix文件名来说,没有“转义”这样的东西。

#2


2  

POSIX filenames don't have a concept of escape characters. 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). 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.

POSIX文件名没有转义字符的概念。没有办法将斜杠作为文件名的元素(当系统使用Unicode呈现文件名时,您可能能够创建一个看起来好像包含斜杠的文件名)。我认为所有其他可打印字符都很好,虽然使用像*和?这样的特殊字符?当人们尝试从shell使用它们时,在文件名中可能会引起问题。

#1


4  

Actually Unix (or more specific the SuS) disallows only the byte values '/' and '\0' in file names. 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(或更具体的SuS)不允许文件名中的字节值'/'和'\ 0'。其他一切实际上都是公平的游戏。确切的(从某种意义上说它们紧跟在后面跟着'/')字符串“。”并且“..”保留给相对路径访问,但它们在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. Yes, newline, any control character, they're all perfectly valid Unix filenames.

当然还有任何数量和顺序的'。'完全允许在Unix文件名中,只要除“/”或“\ 0”之外的其他字符是文件名的一部分。是的,换行符,任何控制字符,它们都是完全有效的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. Which essentially means, a filename is treated as a binary blob key into a key→value database. It also means, that there's no such thing as "escaping" for Unix filenames.

哦,应该注意的是,Unix没有为文件名指定圆顶“解析”方法。这实际上意味着,文件名被视为一个二进制blob键,进入一个键→值数据库。这也意味着,对于Unix文件名来说,没有“转义”这样的东西。

#2


2  

POSIX filenames don't have a concept of escape characters. 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). 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.

POSIX文件名没有转义字符的概念。没有办法将斜杠作为文件名的元素(当系统使用Unicode呈现文件名时,您可能能够创建一个看起来好像包含斜杠的文件名)。我认为所有其他可打印字符都很好,虽然使用像*和?这样的特殊字符?当人们尝试从shell使用它们时,在文件名中可能会引起问题。