Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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_Fopen - Fatal编程技术网

C++ “如何故意失败”;fopen“;

C++ “如何故意失败”;fopen“;,c++,file,fopen,C++,File,Fopen,为了测试我的代码,我需要故意使文件的“fopen”失败。我该怎么做呢。到目前为止,我的代码是: void MeshTrian::init (std::string filename) { if (!sofa::helper::system::DataRepository.findFile(filename)) { msg_error() << "File '" << filename << "' not found." ;

为了测试我的代码,我需要故意使文件的“fopen”失败。我该怎么做呢。到目前为止,我的代码是:

void MeshTrian::init (std::string filename)
{
    if (!sofa::helper::system::DataRepository.findFile(filename))
    {
        msg_error() << "File '" << filename << "' not found." ;
        return;
    }
    FILE *f = fopen(filename.c_str(), "r");
    bool status;
    msg_error_when(!(status=f))<<sofa::helper::message::UnableToOpenFile(filename.c_str());
    if (status)
    {
        readTrian (f);
        fclose(f);
    }
}
void MeshTrian::init(std::string文件名)
{
如果(!sofa::helper::system::DataRepository.findFile(文件名))
{

msg_error()为它提供一个不存在的文件,根据
findFile
方法的工作方式,该文件在您的情况下可能不起作用。正是如此……因此,同名文件应该存在,但在打开时会出错。我尝试压缩文件并剥离“.tar.gz”扩展名。它不起作用:(您可能需要更改文件权限,以便具有写入权限,但不具有读取权限。这样,
findFile
将(希望如此)可以工作,但是
fopen
不会。文件*f不是bool类型,您正在将文件指针分配给bool类型,看起来完全错误,您应该在您的条件下检查“f”而不是“status”。
chmod uga-r filename