7

I'm looping over some file in a directory using Boost FileSystem 3 and I need to cast the filename to a char* for another lib, Unfortunately my C++ foo is lacking, can anyone help ?

  int main(int argc, char* argv[])
    {
      path p (argv[1]);   // p reads clearer than argv[1] in the following code

      try
      {
        if (exists(p))    // does p actually exist?
        {
          if (is_regular_file(p))        // is p a regular file?   
            cout << p << " size is " << file_size(p) << '\n';

          else if (is_directory(p))      // is p a directory?
          {
            cout << p << " is a directory containing:\n";

            typedef vector<path> vec;             // store paths,
            vec v;                                // so we can sort them later

            copy(directory_iterator(p), directory_iterator(), back_inserter(v));

            sort(v.begin(), v.end());             // sort, since directory iteration
                                                  // is not ordered on some file systems

            for (vec::const_iterator it (v.begin()); it != v.end(); ++it)
            {
              cout << "   " << *it << '\n';
       /****************** stuck here **************************/
      // I need to cast *it to a const char* filename 
       /****************** stuck here **************************/
            }
          }

          else
            cout << p << " exists, but is neither a regular file nor a directory\n";
        }
        else
          cout << p << " does not exist\n";
      }

      catch (const filesystem_error& ex)
      {
        cout << ex.what() << '\n';
      }

      return 0;
    }
macarthy
  • 3,074
  • 2
  • 23
  • 24

1 Answers1

2

The expression*it returns an object of type path, so you've to this:

const std::string & s = (*it).string(); 
const char *str = s.c_str(); //this is what you want

Or maybe you want to use other conversion functions, as listed below:

const std::string & string() const;
std::string native_file_string() const;
std::string native_directory_string() const;

Choose whichever you want to use. Read the documentation first as to what each of them returns:

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 2
    The link should probably go to the canonical [boost filesystem documentation](http://www.boost.org/doc/libs/1_47_0/libs/filesystem/v3/doc/reference.html#path-native-format-observers) at boost.org – Fabio Fracassi Jul 26 '11 at 09:22
  • Two questions... First, does `s` survive longer than the expression `const std::string & s = (*it).string();`? Second, does `str` and the memory backing `c_str()` survive longer than the expression `const char *str = s.c_str();`? (This reeks of `stringstream.str()` and the `std::string` that must be copied). – jww May 03 '19 at 01:26
  • @1201ProgramAlarm - Re: the closed question at [Passing Boost path vector items to Crypto++ FileSource function](https://stackoverflow.com/q/55959865/608639)... Do you know if this answer is safe or accurate? The string reference worries me. I spent the better part of two days debugging and learning the `stringstream.str()` lesson. – jww May 03 '19 at 01:29
  • 1
    @jww This is safe, as long as you don't cause the pointer to be retained after the string goes out of scope. I suspect "the `stringstream.str()` lesson" you learned was inaccurate in some respect (some subtle detail may have been overlooked). Perhaps a new question for that? – JaMiT May 18 '19 at 01:53
  • @JaMiT - Here was the problem I encountered when trying to use the pointer from the `string` inside the `stringstream`: [Memory Error with std:ostringstream and -std=c++11?](https://stackoverflow.com/q/21283133/608639). This answer looks like it is doing the same thing (to me). – jww May 18 '19 at 01:59
  • 1
    @jww No, this answer is doing what the answers over there told you to do: assign the temporary to a variable, then call `c_str()` in a separate statement. See also [temporary lifetime extension](https://stackoverflow.com/a/11560472). – JaMiT May 18 '19 at 04:43