Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我们可以将带有空格的路径字符串作为参数传递给fopen()?_C_File Io_Path_Fopen - Fatal编程技术网

C 我们可以将带有空格的路径字符串作为参数传递给fopen()?

C 我们可以将带有空格的路径字符串作为参数传递给fopen()?,c,file-io,path,fopen,C,File Io,Path,Fopen,我正在使用fopen(),我需要打开一个文件,在其中传递一个路径,其中包含空格作为参数。这是我的密码: FILE * pFile; pFile = fopen ("\this folder\myfile.txt","w"); 这样行吗?还是我需要在其中添加一些东西来识别空格?谢谢。空格可以,但您需要转义'\',如下所示 pFile = fopen ("\\this folder\\myfile.txt","w") 如果路径中有空格,则无需执行任何特殊操作 pFile = fopen ("\\

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

FILE * pFile;
pFile = fopen ("\this folder\myfile.txt","w");

这样行吗?还是我需要在其中添加一些东西来识别空格?谢谢。

空格可以,但您需要转义
'\'
,如下所示

pFile = fopen ("\\this folder\\myfile.txt","w")

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

pFile = fopen ("\\this folder\\myfile.txt","w");
应该有用。请注意字符串中所需的双反斜杠。

非pFile=fopen(“\this folder\myfile.txt”,“w”)


应该是pFile=fopen(“\this folder\myfile.txt”,“wb”)

考虑:
rawurlencode()

如果您试图加载外部资源,如
http://domain.com/path/to/file/filename has spaces.php
则可能需要对url进行编码,以避免
fopen()
失败

在这种情况下,实际上应该调用
rawurlencode()
,因为这将把所有必需的字符转换为
%XY
字符,包括空格。常规的
urlencode()
将空格转换为
+
,这样做没有帮助


警告:我还没有在一个庞大的字符串列表上测试过它,因此它可能会产生一系列不同的问题,但它在我能找到的所有情况下都对我有效

为什么不试试呢?最好的办法就是试试看(提示:用\\\`s替换
\\`s后,它就可以工作了,否则第一个字符将是
选项卡
)。我只是想确保这不会首先导致问题。我觉得这是一个非常基本的问题。必须将路径的“\”替换为“\”。