0

I want to read text from a text file like so

Text File: This is some text \nThis is the next line

However when I read it into a string and output it I get something like this This is some text \nThis is the next line

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

Code:

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();
    }
}
Megarev
  • 50
  • 7
  • The ">" string is there because I'll reading many texts from the same text file – Megarev May 25 '20 at 15:32
  • 1
    [Why is !iostream::eof considered bad practice in a loop condition?](https://stackoverflow.com/q/5605125/2027196) – JohnFilleau May 25 '20 at 15:35
  • Probably not exactly the way you have in mind. The `\n` thing is a compile-time escape - it's the compiler that parses the string literals in source code and converts them into (in this case) a newline character. However, you can of course write the code to do the same thing the compiler does. – 500 - Internal Server Error May 25 '20 at 15:35
  • You have to write some code process the escape sequences. – HolyBlackCat May 25 '20 at 15:35
  • Ok but Why wouldn't there be a new line in the output – Megarev May 25 '20 at 15:36
  • The "escape character" concept only applies to literal characters in the source code; it lets you write literal character that you normally can't (for various reasons). The backslash in data is not an escape character, it's just a backslash. – molbdnilo May 25 '20 at 15:37
  • @JohnFilleau: Apologies for the clumsy wording. – 500 - Internal Server Error May 25 '20 at 15:37
  • @500 I just got what you meant – JohnFilleau May 25 '20 at 15:37
  • So I have to manually insert '\n' in the string? – Megarev May 25 '20 at 15:38
  • @Mega how is this file generated? Are you just hand-jamming it from vim or notepad or something? – JohnFilleau May 25 '20 at 15:38
  • If you want to interpret that character sequence as a line break, you need to translate it. (That's what the compiler does with literals.) – molbdnilo May 25 '20 at 15:39
  • @JohnFilleau I am writing the file in notepad – Megarev May 25 '20 at 15:39
  • If you want a newline sequence in your file, you press Enter or Return to insert one. The text file program will convert that to the OS-specific newline sequence (`'\n'` on unix-y system, `'\r\n'` on windows). Not sure how in Notepad, but if you have Notepad++ you can highlight you ASCII text and select `Plugins > Converter > ASCII to HEX` to translate to all of your typed text into the hex equivalent. That might help you see what's going on. – JohnFilleau May 25 '20 at 15:40
  • @Megarev if you are writing the file yourself why not write it with a `endl`? And maybe use it again while reading from the file. – Umair May 25 '20 at 15:48
  • Ok I wanted to write the string directly along with new lines, I'll just make a vector and write it individually – Megarev May 25 '20 at 15:53

0 Answers0