Sign in to follow this  
suliman

the "\n" dilemma (linebreak) -reborn

Recommended Posts

suliman    1652
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

Share this post


Link to post
Share on other sites
SiCrane    11839
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?

Share this post


Link to post
Share on other sites
visitor    643
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?

Share this post


Link to post
Share on other sites
rip-off    10976
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.

Share this post


Link to post
Share on other sites
kinks    122
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.

Share this post


Link to post
Share on other sites
Storyyeller    215
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?

Share this post


Link to post
Share on other sites
nerd_boy    720
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? <_<

Share this post


Link to post
Share on other sites
yewbie    677
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"

Share this post


Link to post
Share on other sites
moff    100
Post your code- '\n' should not be interpreted as two separate characters, so there is something wrong with this picture. '\n' is a special character that is read as a single character, not a '\' and an 'n'.. in fact, you would need to use the special '\\' character to even get a '\' by itself.. You must be using the string class, and have some error in what you are telling it to do versus what you want it to do, because I can't imagine having this issue with a regular array of characters.

Share this post


Link to post
Share on other sites
Kylotan    9854
Quote:
Original post by nerd_boy
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? <_<

That looks to me like exactly what he needs to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this