【问题标题】:Boost not finding fileBoost找不到文件
【发布时间】:2015-01-17 17:40:39
【问题描述】:

我想使用 Boost 的文件系统功能。我试试

cout << boost::filesystem::file_size(fname.c_str()) << endl;

fname="file.txt"; 哪里出错了

boost::filesystem::file_size: No such file or directory

我确信我有正确的道路,因为system("cat file.txt") 有效。我检查了该文件夹不是 NFS;它是 NTFS。

我在 Windows 7 机器上的 cygwin 上使用 g++。

编辑:我也试过了

cout << boost::filesystem::file_size(fname);

并使用fname="./file.txt"

在 makefile 中,我使用 -lboost_system -lboost_filesystem-I /usr/local/opt/boost/include -L /usr/local/opt/boost/lib 进行链接

更新:我对其进行了更改,因此文件开头有一个using namespace boost::filesystem;,并删除了file_size()前面的boost::filesystem::"。现在我在使用fname="file.txt" 时遇到了同样的错误,但如果我使用fname="./file.txt",我会收到一个新错误:

boost::filesystem::file_size: Operation not permitted

我尝试过不同类型的文件(.txt、.dat、.cpp)

附带的教程程序tut1.cpp 使用相同的语法工作,所以我认为这可能是一个编译问题。我试图找到编译 tut1.cpp 的文件(可能是 makefile),但没有成功。

tut1.cpp 看起来像这样:

//  filesystem tut1.cpp  ---------------------------------------------------------------//

//  Copyright Beman Dawes 2009

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: tut1 path\n";
    return 1;
  }
  std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
  return 0;
}

【问题讨论】:

    标签: c++ boost directory cygwin boost-filesystem


    【解决方案1】:

    fname 是路径吗?因为,那么,只需删除 .c_str() 成员调用。

    可能是访问器函数添加了一些操作系统特定的引用或转义。 (example on linux)

    您可以分配给 std::string 以便您可以在调试器中观察该值。

    【讨论】:

    • 其实我是后来想到的;没有c_str(),错误仍然存​​在。也许你是对的,我必须使用调试器
    猜你喜欢
    • 2018-01-21
    • 2014-09-25
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    相关资源
    最近更新 更多