the "\n" dilemma (linebreak) -reborn

Started by
10 comments, last by Kylotan 14 years, 11 months ago
hi im parsing txt files and uses tables for data for objects/units/buildings in my game. I wanna insert linebreaks in the strings i read from txt file but doesnt know how. \n doesnt work. The string just gets the chars \ and n <enter-key> does work but i cannot use it as it messes up the "tables" i have in my txt-files (there's a lot of entries so i need to keep it clear). Every entry has one line of data+strings in the txt-file so i cannot use the enter key to tell it where in my strings i want a new line. Thanks for any help Erik
Advertisement
Did you try using \n and then manually replacing the \n sequence with a new line in your code after reading it from the file?
im pretty bad with strings...
There is no built-in functionality for this then?
E
You could probably use any character in the file that won't be used in the data. Replacing one character with '\n' is probably simpler than replacing a sequence of two characters.

What language are you using in the first place?
Quote:Original post by suliman
im pretty bad with strings...
There is no built-in functionality for this then?
E

You can use std::string::replace(), at least if the only sequence you are interested in supporting "\n". If you want to be able to use support escaping quotes or other characters, you will need a bit more work.
No, there is no 'built-in' functionality for this as what you want is arbitrary. You are going to have to either come up with a different way to seperate fields (such that linebreaks alone do not have any meaning other than to start a new line of text) or replace a sequence of characters with linebreaks after reading the data in (see above posts).

One example of the former might be to have every entry terminated by a given character sequence (say "$\n", rather than "\n") so that a newline by itself is read in as normal. You would then likely want to read in the file as binary data rather than line by line to make things easier. The only real advantage to that is that editting the file by hand (ie. if these are descriptions of some sort) might be easier with a normal text editor. However, this will make it so it's harder to distinguish entries visually at first glance, but ideally if that's your goal, you may want to write a sort of 'resource editor' to do this more effectively (a very long line of text is not very pleasant to work with).

If right now your main goal is making the implementation simple, you're probably better off simply representing linefeeds with some arbitrary character sequence and replacing it upon reading in the data.
Is there anyway to read in the characters as binary data? You could just use 0x0D0A, 0x0D, or 0x0A depending on your preference.

Or am I misunderstanding the problem?
I trust exceptions about as far as I can throw them.
He's already using a new line as a delimiter.
A wee bit tired, so I might be confused as to what the bloke wants, but here goes.

Isn't he saying that the *characters* '\'+'n' are what're showing up when he *types them in*, and not '\n' like he is wanting?

So isn't what he is wanting something along the lines of replace("\\n","\n") or somesuch?

Or am I completely off mah rocker? <_<
I think he is parsing out a text file and wants to read line breaks/new lines from the file for instance

Hello there \nnext line

if parsed would output

"Hello there
next line"

but when he reads it from his file it outputs

"Hello there \nnext line"

This topic is closed to new replies.

Advertisement