我们可以将其中包含空格的路径字符串作为参数传递给 fopen() 吗?

Tom*_*mmy 3 c file-io fopen path

我正在使用,我需要打开一个文件,在其中fopen()传递一个带有空格的路径作为参数。这是我的代码:

FILE * pFile;
pFile = fopen ("\this folder\myfile.txt","w");
Run Code Online (Sandbox Code Playgroud)

这样可以吗?还是我需要在其中添加一些内容来识别该空间?谢谢。

Roh*_*han 5

空间可以,但你需要逃离'\',因为

pFile = fopen ("\\this folder\\myfile.txt","w")
Run Code Online (Sandbox Code Playgroud)


Sat*_*atA 5

如果路径中有空格,则无需执行任何特殊操作。

pFile = fopen ("\\this folder\\myfile.txt","w");
Run Code Online (Sandbox Code Playgroud)

应该管用。请注意字符串中所需的双反斜杠。