如果斜杠“/”放在目录名称之前,则 fopen 在 C 中失败
fopen fails in C if slash '/' is put before directory's name
如果我用以下任何一种方式在 C 中打开文件,那么 fopen
工作正常。
fopen("file.txt", "w");
fopen("/file.txt", "w");
fopen("dir/file.txt", "w");
如果我如下在目录名称前放置斜杠“/”(或在 Windows 的情况下为“\”),则 fopen
失败(returns NULL)。
fopen("/dir/file.txt", "w");
它发生在 Windows (MSVC) 和 Linux 上。这是什么原因呢?我应该删除起始斜杠“/”吗?
您需要添加“.”在“/”前面
例如:fopen("./dir/file.txt", "w");
如果您以“/”开头,它将从根目录开始。这就是 fopen returns null
的原因
如果我用以下任何一种方式在 C 中打开文件,那么 fopen
工作正常。
fopen("file.txt", "w");
fopen("/file.txt", "w");
fopen("dir/file.txt", "w");
如果我如下在目录名称前放置斜杠“/”(或在 Windows 的情况下为“\”),则 fopen
失败(returns NULL)。
fopen("/dir/file.txt", "w");
它发生在 Windows (MSVC) 和 Linux 上。这是什么原因呢?我应该删除起始斜杠“/”吗?
您需要添加“.”在“/”前面
例如:fopen("./dir/file.txt", "w");
如果您以“/”开头,它将从根目录开始。这就是 fopen returns null
的原因