简体   繁体   English

即使文件在同一目录中,ifstream 也找不到文件

[英]ifstream won't find file even though it's in the same directory

I've got a method to read a vector of bools from a file:我有一种从文件中读取布尔向量的方法:

std::vector<bool> OPCConnector::getAlarmVector() {
    std::vector<bool> data;
    std::ifstream DataFile(filepath);

    if (DataFile) {
        bool value;

        while (DataFile >> value) {
            data.push_back(value);
            std::cout << value;
        }
    }
    
    return data;
}

The filepath variable is an object property that is assigned through the constructor: filepath变量是通过构造函数分配的对象属性:

OPCConnector::OPCConnector(std::string fpth) {
    filepath = fpth;
}

And in the main() function, the constructor is called:main()函数中,构造函数被调用:

std::vector<bool> activations;
std::string filepath = "alarmes.txt";
OPCConnector opcc = OPCConnector(filepath);
activations = opcc.getAlarmVector();

Now, I've checked what the folder of the executable is via GetModuleFileNameA() , and I made sure that the file is in the same directory and has the same name (also, I made sure that the extension isn't part of the file name, like "alarmes.txt.txt" ).现在,我已经通过GetModuleFileNameA()检查了可执行文件的文件夹是什么,并确保该文件位于同一目录中并具有相同的名称(此外,我确保扩展名不是文件名,如"alarmes.txt.txt" )。

I debugged the first method getAlarmVector() and it never gets past the if (DataFile) condition, as if it won't find file.我调试了第一个方法getAlarmVector()并且它永远不会超过if (DataFile)条件,就好像它找不到文件一样。

I run the code using Visual Studio 2019, and nothing happens.我使用 Visual Studio 2019 运行代码,但没有任何反应。 The vector remains empty.向量保持为空。 Error is No such file or directory.错误是没有这样的文件或目录。

项目目录设置

Default working directory is $(ProjectDir) and it's exactly where my file is.默认工作目录是$(ProjectDir) ,它正是我的文件所在的位置。

Edit: I've also tried using both relative and absolute paths, none work.编辑:我也尝试过使用相对路径和绝对路径,但都不起作用。

Edit 2: I've also checked the directory using GetCurrentDirectory() and copied the .txt file there too, and it isn't working.编辑 2:我还使用GetCurrentDirectory()检查了目录并将.txt文件复制到那里,但它不起作用。

SOLUTION: Strangely enough, I deleted the file and created it again with the same name, and it worked.解决方案:奇怪的是,我删除了该文件并使用相同的名称再次创建它,并且它起作用了。 Thanks for the answers.感谢您的回答。

My guess: your current working directory isn't what you think it is, especially if you're running from an IDE.我的猜测:您当前的工作目录不是您认为的那样,尤其是当您从 IDE 运行时。 I know of several IDEs where the current working directory is some build directory (it varies by IDE) unless you specifically change it.我知道有几个 IDE,其中当前工作目录是某个构建目录(它因 IDE 而异),除非您专门更改它。

I'm fairly sure Visual Studio is one such IDE.我相当确定 Visual Studio 就是这样一种 IDE。


Here's a tiny example program I wrote; 这是我编写的一个小示例程序;
$ g++ --std=c++17 Foo.cpp -o Foo && Foo
File opened.

Compile and run it:编译并运行它:

 $ g++ --std=c++17 Foo.cpp -o Foo && Foo File opened.

Current folder and folder-of-exe-file are different things (sometimes).当前文件夹和文件夹的 exe 文件是不同的东西(有时)。 Try to specify full name of file (with disk, all folders, etc.).尝试指定文件的全名(包括磁盘、所有文件夹等)。

You can check errors of file open operation by calling您可以通过调用来检查文件打开操作的错误

if (!DataFile) { ... }

You can get an error code (and get a description of error in internet) if you use C-function fopen .如果您使用 C 函数fopen您可以获得错误代码(并在互联网上获得错误描述)。 If open is failed, you get the nullptr as result of fopen and errno will contain code of error.如果打开失败,您将获得作为 fopen 结果的 nullptr,而errno将包含错误代码。

The std::filesystem library can help you resolve file and path related issues. std::filesystem库可以帮助您解决文件和路径相关问题。

#include <filesystem>


    // (in some function)
    std::filesystem::path filepath = "alarmes.txt";
    if ( !exists(filepath) )
    {
        std::cout << "File path " << filepath << " at absolute location "
                  << absolute(filepath) << " does not exist\n";
    }

See it on Compiler Explorer在编译器资源管理器上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM