Line breaks in plain text?

Anyone know how to code a line break in a plain text file (if it’s any help, the file extn is .txt).

Whitespace, including line breaks, are preserved in text files, so if you’re hardcoding it, simply add a regular line break.

If you have to generate the file, you can add a new line with \ .

(Note that, technically, you should use the 0x0d 0x0a character sequence in single-byte character sets, and 0x000d 0x000a sequence in double-byte character sets, but I have never encounted any problems with the single \ ).

Should according to whom?

Note that 0x0a and 0x000a are exactly the same number. It doesn’t matter whether you specify the number as 10 or 0010 or 00000000010 it is still TEN. Adding extra zeros to the front of a number no matter what the number base doesn’t change it.


is just a convenient shortcut that has been assigned so that people don’t have to remember that a new line character (which is what the n represents) is the tenth character in the ASCII and Unicode character sets.

All the 0x on the front of the number means is that it is base 16 instead of base 10.

0 1 2 3 4 5 6 7 8 9 a b c d e f 10

Simon
RFC 2616, section 3.7.1 and RFC 959, section 3.1.1.1. Probably also others. But, as I also mentioned, \ will to the trick. It’s a bit like how browsers should display a lot of >'s around the interwebs, because of faux-XHTML and NET-enabled HTML.

Stephen
I know, but in the (probably very unlikely) event that the original poster decides to code the text files in a hex editor in Unicode, and used 0x0a 0x0d in stead of 0x000a 0x000d, it would give unexpected results.

Not in character encoding it isn’t… Well, it is and it isn’t… You send 0x000A to a 8 bit character set, you’ll get null followed by a carriage return, instead of just a null – on little endian systems. Big endian it’s the other way around. Null could be bad, as most C strings are null terminated. Could result in the entire rest of the file not even displaying if you sent the word-length version to the wrong encoding.

Oh, and it would help if we were to say what the characters ARE instead of just spouting off their numbers. Carriage return (0x0D - /r) and Line feed (0x0A - /n)… which stem back to the typewriter, teletype and serial terminal days.

TECHNICALLY by what the characters mean in ASCII, line feed should ONLY move the cursor down without setting it to beginning of line, which is why DEC PDP, CP/M, TOS, and anything DOS based requires both /r/n. CR+LF.

Apple (from the II right up to MacOS-9), old Trash-80’s, use carriage return only… This was mostly done originally to save that one byte.

Naturally like everything else *nix, they use the one that makes the least sense, linefeed only. This actually makes *nix incompatible with a lot of older terminals unless you tell it what the terminal is and have software translate it.

Thankfully across most systems the unrecognized character is usually ignored – and at worst you just get double-spacing… so CR+LF works on 99% of the world. Unless you’re messing around with old Sinclairs it’s unlikely to be an issue - though it is why most editors still let you chose how newlines are handled.

quote for truthiness:

Of course when you need numbers to be a specific length then of course you need to pad them with zeros. So it should only be if you pad with zeros unnecessarily that you would get a problem as then it can be misidentified as being multiple characters.

Not including the leading zeros should mean that you don’t need to concern yourself with little-endian versus big-endian as the system should add the zeros in the right place provided that the program is intelligent enough to know how numbers work.

An assumption I wouldn’t make about any compiler or interpreter – but I’m an assembly language coder at heart; where all things have to be precise and specific…

Assuming the compiler, interpreter or assembler even lets you do it in the first place, which is where I think a lot of the real problem lies in things like PHP and HTML – WAY too permissive and allows you to make too many assumptions; net result being sloppy coding and people making poor decisions because they never learn how it works under the hood.

Many thanks for the ideas on this - it’s appreciated. I tried \r
but they actually came out in the emails exactly like that?

If it helps, it’s for auto emails (activation) from a phpbb forum.

Can we see the code you are inserting it into?

Were you using single quotes or double quotes to add them? They only encode from double quotes… (IMHO the only LEGITIMATE reason to use double quotes on ANYTHING)

Though yeah, I think we need to see some code.

But then, with single quotes, those line breaks could just as easily by simply adding the line break to the text.

It’s all just plain text, no code.

So should it be :

"\\r\
"

or

'\\r\
'

As originally stated, if it’s plain text, you can simply add the line breaks using [Return].

Yep, and that doesn’t work!

If you are using double quotes (") and the file is being parsed as PHP and the escape characters (\\r\ ) are still showing, then it has to do with how the scrips handles the files. My guess (based on your saying that it’s phpBB) would be that the code is not being parsed as PHP at all, but rather as HTML. Does it work if you use <br>?

Silly question, but what are you doing with said plaintext file? Is some editor you are trying to use stripping them? Are they not showing up when you echo…

Being you asked in the HTML forum, are you outputting it in HTML? If so, you need to put it inside PRE tags for the carriage returns to show… That or replace all
with <br />… or if it’s inside some other tag, add white-space: pre; in the CSS.

HTML is whitespace neutral – so all carriage returns and spaces between words are collapsed into single spaces – that’s the only situation I can think of where what you describe would happen.

Though that’s a wild guess. Again, without seeing the file and what you are trying to do with the file – we’re all just guessing. Educated guesses, but still just guessing.

I am also facing this problem of inserting a linefeed LF CR in geany / gedit linux. In geany, when I open a file I can see the characters LF for line feeds. But which shortcut key we have to use for inserting the visible letters LF ?

Purpose of above: Use LF or LFCR as delimiter for exploding a string into small pieces using PHP explode function.