键入 'File' 以键入 'std::string'

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

本文关键字:std string File 键入      更新时间:2023-10-16

我想把这个文件浏览器加载的txt文件的内容放到字符串'fileLoadToString'中。使用引号中的路径时,转换有效。

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

这是出现"没有可行的重载="错误消息的地方。如何以正确的方式转换?

在主进程中,我想将字符串加载到流中以进行标记化和进一步分析。

std::ifstream inf(fileLoadToString);

任何帮助总是非常感谢,谢谢。

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();
}

>myChooser.getResult返回一个juce::File对象。 您需要从该文件对象读取。 如果fileLoadToString是类型 juce::String ,你可以只写:

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

如果它是不同类型的,则必须转换 readFileAsString .