Boost not finding file

957 Views Asked by At

I want to use Boost's filesystem functions. I try

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

where fname="file.txt"; and I get the error

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

I am sure I have the right path because system("cat file.txt") works. I checked that the folder is not NFS; it is NTFS.

I am using g++ on cygwin on a Windows 7 machine.

EDIT: I also tried

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

and using fname="./file.txt"

In the makefile I'm linking using -lboost_system -lboost_filesystem and also using -I /usr/local/opt/boost/include -L /usr/local/opt/boost/lib

UPDATE: I changed it so there is a using namespace boost::filesystem; at the beginning of the file and removed boost::filesystem::" in front of file_size(). Now I have the same error using fname="file.txt" but if I use fname="./file.txt" I get a new error:

boost::filesystem::file_size: Operation not permitted

I have tried different kinds of files (.txt, .dat, .cpp)

The tutorial program tut1.cpp that comes with it and uses the same syntax works, so I'm thinking it could be a compiling problem. I have tried to find the file (presumably a makefile) compiling tut1.cpp with no luck.

tut1.cpp looks like this:

//  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;
}
1

There are 1 best solutions below

1
On

Is fname a path? Because, then, just drop the .c_str() member invocation.

It might be that the accessor functions add some Operating System Specific quoting or escaping. (example on linux)

You might assign to a std::string so you can observe the value in a debugger.