23

I'm using File Explorer to monitor file size in Windows.

I have a .dat file and when I type normal characters like letters the file size increases by 1 byte but when I press enter the file size increases by 2 bytes.

Why does the file size increase by 2 bytes when I press enter?

Laurel
  • 160
devpa
  • 882

2 Answers2

68

Windows uses both Carriage Return \r and Line Feed \n to signify a new line, unlike UNIX (which uses \n).

See also Does Windows carriage return \r\n consist of two characters or one character?
and Why does Windows use CR LF?

JoSSte
  • 403
Tetsujin
  • 49,589
5

Because pressing "Enter" does not write one character, as you might expect, but two. Those two characters are, in hexadecimal, 0x0D and 0x0A. The first one returns the cursor to the beginning of the line, and the second moves the cursor one line below.

  • 7
    Certainly "The first one returns the cursor to the beginning of the line, and the second moves the cursor one line below" encapsulates the traditional meaning of CR and LF (for example, if you are programming in FORTRAN), but their use in Windows text files is more historical and traditional than anything else: https://devblogs.microsoft.com/oldnewthing/20040318-00/?p=40193 – Flydog57 Feb 27 '23 at 16:44
  • 1
    @Flydog57 that is also their meaning in various console emulators, often used to make progress indicators. – OrangeDog Feb 27 '23 at 23:15
  • 2
    @OrangeDog In raw mode, yes. In the more usual text mode, \n will move to the beginning of the next line. (\r nearly always means carriage return.) – wizzwizz4 Feb 27 '23 at 23:22
  • @Flydog57 this has all to do with the OS (specifically, Windows' heritage back to TOPS-10), and nothing to do with FORTRAN. – RonJohn Feb 28 '23 at 15:25
  • 1
    @RonJohn: That was my point. DEC OSes (from TOPS-10 onwards) used CR/LF and it became the model for CP/M and MS-DOS (and eventually Windows). But, the use of CR or LF has pretty much evaporated. From a programming point of view, I haven't seen CR or LF used by themselves since I worked in FORTRAN in the 70s. CR was great for overprinting a line (or parts of a line) of text, to make the text appear bold. I never used LF on its own; when it did show up, it was usually a bug. In FORTRAN, if I remember correctly, they were traditionally not considered a pair; you specified them separately. – Flydog57 Feb 28 '23 at 17:09
  • @Flydog57 This is how you could underline, strike-though and tildes and accents to letters as well, right? I always assumed the lineage went back to teletype. I just learned that's where 'TTY' comes from. – JimmyJames Feb 28 '23 at 20:39
  • Teletypes are hard to type on. There's about a one inch (2 or 3 cm) throw on each key press. The really old ones hare shift-in and shift-out (i.e., like some soft keyboard, if you want to type "Hello", you'd type <shift-in>H<shift-out>ello. Yeah, I'm that old – Flydog57 Feb 28 '23 at 20:42