Domanda

someone just helped me get a filename from a directory with boost using

    if (exists(p))    // does p actually exist?
    {

        if (is_directory(p))      // is p a directory?
        {
            copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
                ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is

        }
    }

but I want it in a string not cout

how can i capture that copy?

È stato utile?

Soluzione

You can copy the content to a std::ostringstream and retrieve a copy of the buffer using str():

std::ostringstream buf;

std::copy(directory_iterator(p), directory_iterator(),
          std::ostream_iterator<directory_entry>(buf, "\n"));

std::string content(buf.str());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top