fopen("filename", "wb") returns 空
fopen("filename", "wb") returns null
这样的代码:
#include <iostream>
using namespace std;
int main(){
FILE* to = NULL;
to = fopen("cpp", "wb");
if(to != NULL)
cout << 1 << endl ;
cout << 2 << endl;
}
使用 g++
和选项 -o
编译,然后 run.Returns“2”。要使用 ls -al
查看 cpp 文件夹:drwxr-xr-x 2 anyone staff 64 6 8 08:31 cpp
为什么是 returns“2”。为什么我无法打开文件夹进行写入?
fopen 用于打开文件而不是文件夹,您确定 cpp 是一个文件夹还是您弄错了?
发生这种情况是因为您正试图打开一个文件夹进行写入。根据 fopen
documentation,你得到 [EISDIR]
错误:
[EISDIR]
The named file is a directory and mode requires write access.
你可以通过打印 strerror(errno))
:
来判断你得到了什么错误
cout << strerror(errno)) << endl;
这样的代码:
#include <iostream>
using namespace std;
int main(){
FILE* to = NULL;
to = fopen("cpp", "wb");
if(to != NULL)
cout << 1 << endl ;
cout << 2 << endl;
}
使用 g++
和选项 -o
编译,然后 run.Returns“2”。要使用 ls -al
查看 cpp 文件夹:drwxr-xr-x 2 anyone staff 64 6 8 08:31 cpp
为什么是 returns“2”。为什么我无法打开文件夹进行写入?
fopen 用于打开文件而不是文件夹,您确定 cpp 是一个文件夹还是您弄错了?
发生这种情况是因为您正试图打开一个文件夹进行写入。根据 fopen
documentation,你得到 [EISDIR]
错误:
[EISDIR]
The named file is a directory and mode requires write access.
你可以通过打印 strerror(errno))
:
cout << strerror(errno)) << endl;