Search code examples
c++stringfilestreamjuce

Type 'File' to Type 'std::string'


I want to put the contents of a txt file loaded by this file browser into the string 'fileLoadToString'. The conversion works when using the path in quotations.

FileChooser myChooser ("Please select the moose you want to load...",
                               File::getSpecialLocation (File::userHomeDirectory),
                               "*.txt");
        if (myChooser.browseForFileToOpen())
        {
            File theTextFile (myChooser.getResult());
            fileLoadToString = theTextFile;

        }

Here is where the 'No viable overloaded =' error message occurs. How do I convert in the correct way?

Within the main process, I want to load the string into a stream for tokenizing and further analysis.

std::ifstream inf(fileLoadToString);

Any help is always much appriciated, thank you.

File FileChooser::getResult() const
{
    // if you've used a multiple-file select, you should use the getResults() method
    // to retrieve all the files that were chosen.
    jassert (results.size() <= 1);

    return results.getFirst();
}

Solution

  • myChooser.getResult returns a juce::File object. You need to read from that file object. If fileLoadToString is of type juce::String, you can just write:

                File theTextFile(myChooser.getResult());
                fileLoadToString = theTextFile.readFileAsString();
    

    If it is of a different type, you will have to convert the result of readFileAsString.