Boot check file exist with path

Hello everyone,
I have a problem to get a true value from the following argument:

string path="c:\\test\test.txt";
boost::filesystem::exists(path) return files

Any suggestion?

thanks
closed account (E0p9LyTq)
string path="c:\\test\\test.txt";
still does not work. I want to indicate if the file exist inside an arbitrary path and not just only in the project folder.
Last edited on
closed account (E0p9LyTq)
I suspect you are not creating a Boost path object to pass to Boost's filesystem functions. Passing a C++ string won't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>

int main()
{
   std::string test = "C:\\test\\test.txt";
   boost::filesystem::path p(test);  // avoid repeated path construction below

   if (exists(p))    // does path p actually exist?
   {
      if (is_regular_file(p))        // is path p a regular file?
      {
         std::cout << p << " size is " << file_size(p) << '\n';
      }
      else if (is_directory(p))      // is path p a directory?
      {
         std::cout << p << " is a directory\n";
      }
      else
      {
         std::cout << p << " exists, but is not a regular file or directory\n";
      }
   }
   else
   {
      std::cout << p << " does not exist\n";
   }
}


"C:\test\test.txt" size is 788


Your path string could also be: std::string path = "C:/test/test.txt";
Last edited on
Topic archived. No new replies allowed.