Why text drawing ignores \n (don't make new line but chars drawing) from the UTF8 strings?

I am filling a vector of strings (lines) with the lines of a txt file, encoded with UTF8 and latin (çòá) characters.

The file content have lines ending without “explicit” \n at lines end:
ohplaces.txt

Congratulations!
Today is your day.
You’re off to Great Places!
You’re off and away!

I used the snippet from the example to put each line into a string pushed into the vector:
openFrameworks/examples/input_output/loadTextFileExample/loadTextFileExample

    vector <string> seussLines;

    ofBuffer buffer = ofBufferFromFile("ohplaces.txt");
    
    if(buffer.size()) { 

	for (ofBuffer::Line it = buffer.getLines().begin(), end = buffer.getLines().end(); it != end; ++it) {

	    string line = *it;

            // copy the line to draw later
            // make sure its not a empty line
            if(line.empty() == false)
            {
                seussLines.push_back(line);
            }     
            
            // print out the line
            cout << line << endl;
        }
    }   

Sometimes I want to insert into the txt lines one \n, not to be printed but to make a new line/jump.
The problem is that ofxTrueTypeFont and ofxFonstash are printing the \n chars.

If I fill the vector of strings with the same lines that the txt (including \n) but hardcoded, not loading from the file. The text is drawn fine with the new lines too.

I don’t know if I must use some UTF8 helpers or what I am missing…
Aany idea of how to solve this?

I’m not entirely clear how you’re inserting \n, or how you’re printing - I haven’t used cout to print in OF before, so that may be my knowledge gap.

My first thought may not be appropriate, but perhaps you’re dynamically adding the string “\n” to a string, instead of adding the single character ‘\n’?

thanks, @Drazinut,
for printing, I mean drawing with a font object, like using ofTrueTypeFont or ofxFontstash.

when adding \n into the .txt with my editor like that:
ohplaces.txt

Congratulations!
Today is \nyour day.
You’re off to \nGreat Places!
You’re off and \n away!
The \n letters are drawn.

But when I test to push the lines to the vector into one string it works: it does the line break and do not draws the \n chars:

std::string line = "You’re off to \nGreat Places!";
seussLines.push_back(line);

My problem is that I want to read the text from the txt file, I can’t do this hardcode manually.

Oh right, that makes sense. Ok. So it’s the difference between how source code string constants are interpreted by the compiler, and how ofBufferFromFile() converts strings in the file into ofBuffer content. The latter treats \n as an \ character and an n character. You would want to put an actual newline character in the text file, or add code to convert \n to a newline character after you load it.

1 Like

this have sense, thanks, but
How can I put the character ‘\n’ to be readed as one single char instead this two chars?

I tried to use \n, /\n …etc without success.

I use Sublime to edit the text, and openend as UTF8 and looks fine.

Why not use the Enter key in the editor?

Is it that you want to be able to load from a text file, and end up with a vector of strings, some of which have newline characters in them?

Why not use the Enter key in the editor?
because each line must be drawn into a single text box.
If I make a “hard” new line, then I’ll get two boxes for the same line.

(I am using ofxFontstash drawing a column with variable width and auto line break…etc)

I may be missing knowledge of some common technique for doing what you want, but what I would do is add some code that parses each line. That is, go ahead and use \n or any other character or sequence of characters you know you’ll never want to write, and then before you print each line, write code that converts that character to a \n in the string, something like:

line = line.replace("\n", ‘\n’);

or whatever the actual syntax would be - I can look it up if you like.

1 Like

thanks @Drazinut, nevermind, i’ll check tomorrow your tip and I will report here.

…It seems that more sugar it’s required, because replace handles strings not chars…
line = line.replace("\n", '\n');
Error No matching member function for call to

Yeah, that was pseudocode.

Try:

ofStringReplace(line, “\\n”, “\n”);

1 Like