首页 > 解决方案 > 我们可以从文本文件中读取转义字符吗?

问题描述

我想像这样从文本文件中读取文本

文本文件: This is some text \nThis is the next line

但是,当我将其读入字符串并输出时,我会得到类似这样的结果 This is some text \nThis is the next line

//Expected Output
This is some text
This is the next line

代码:

void LoadTexts(const std::string& filepath) {
    std::ifstream reader(filepath);

    if (reader.is_open()) {

        while (!reader.eof()) {

            std::string str;

            while (true) {

                std::string readStr;
                //reader >> readStr;
                std::getline(reader, readStr);
                if (readStr == ">") break;

                str += readStr;
            }

            texts.push_back(str);
        }

        reader.close();
    }
}

标签: c++

解决方案


推荐阅读