c++ - boost 找不到文件

标签 c++ boost directory cygwin boost-filesystem

我想使用 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/库

更新:我改变了它,所以在文件的开头有一个 using namespace boost::filesystem; 并删除了前面的 boost::filesystem::" file_size()。现在我在使用 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;
}

最佳答案

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

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

您可以分配给 std::string,这样您就可以在调试器中观察该值。

关于c++ - boost 找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029810/

相关文章:

c++ - 尽管使用 -fPIC 进行编译,但链接器错误 "relocation R_X86_64_PC32 against undefined symbol"

c++ - 如何使用 boost::multi_index 组合键

c++ - 使用子模块应用程序编译 Modular Boost 1.55.0

c++ - 从 Math C++ 获取美分

c++ - CreateWindowEx 失败

java.nio.file.Paths - 如何在迭代时跳过无法访问的文件/文件夹

regex - Mercurial/.hgignore - 如何忽略除文件夹内容之外的所有内容?

linux - 我的主目录与 "/var/www/"Apache Linux 相同

c++ - 有选择地内联函数(用于调试目的)?

c++ - 使用带有自定义图形的 r_c_shortest 路径( boost )