For some reason, newline characters seem to be using 2 bytes in text files instead of one. Is this normal? When I open a text file and look at the size, I get one more for every character I add, except for newlines, which add 2 bytes.

Recommended Answers

All 4 Replies

The new line in the text file is operating-system dependent. *nix uses '\n', MAC uses '\r', and MS-DOS/MS-Windows uses two characters "\r\n". Which is why text files that are transferred between operating systems are often run through a translator to convert from one format to another.

ahh, carriage return... What exactly does \r do in text files? How is that typically interpreted by the font? I think in terminals, it goes back to the beginning of the line without going down to a new one, but I've seen it displayed differently in different terminals. Some will need both, and some automatically do the newline when they display a \r.

It has little, if anything, to do with terminals. Just a method of terminating a text line in text files. I don't know why MAC and MS-DOS didn't follow *nix's lead and just save '\n' in the text file -- would seem the logical thing to do since most (but not necessarily all) lines read by fgets() terminate with '\n'. MAC and MS-DOS/MS-Windows os just have to do a little extra work to maintain that standard.

I don't know why MAC and MS-DOS didn't follow *nix's lead and just save '\n' in the text file -- would seem the logical thing to do since most (but not necessarily all) lines read by fgets() terminate with '\n'. MAC and MS-DOS/MS-Windows os just have to do a little extra work to maintain that standard.

I do believe that Mac OS X uses just \n now, as it's using the FreeBSD kernel as the underlying technology for the operating system, and because of that, is POSIX-compliant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.