{"id":40233,"date":"2004-03-16T07:00:00","date_gmt":"2004-03-16T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/03\/16\/why-do-text-files-end-in-ctrlz\/"},"modified":"2025-02-02T17:10:54","modified_gmt":"2025-02-03T01:10:54","slug":"20040316-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040316-00\/?p=40233","title":{"rendered":"Why do text files end in Ctrl+Z?"},"content":{"rendered":"<p>Actually, text files don&#8217;t need to end in Ctrl+Z, but the convention persists in certain circles. (Though, fortunately, those circles are awfully small nowadays.)<\/p>\n<p>This story requires us to go back to CP\/M, the operating system that MS-DOS envisioned itself as a successor to. (<a title=\"Why does the x86 have so few registers?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040105-00\/?p=41203\">Since the 8086 envisioned itself as the successor to the 8080<\/a>, it was natural that the operating system for the 8086 would view itself as the successor to the primary operating system on the 8080.)<\/p>\n<p>In CP\/M, files were stored in &#8220;sectors&#8221; of 128 bytes each. If your file was 64 byte long, it was stored in a full sector. The kicker was that the operating system tracked the size of the file as the <u>number of sectors<\/u>. So if your file was not an exact multiple of 128 bytes in size, you needed some way to specify where the &#8220;real&#8221; end-of-file was.<\/p>\n<p>That&#8217;s where Ctrl+Z came in.<\/p>\n<p>By convention, the unused bytes at the end of the last sector were padded with Ctrl+Z characters. According to this convention, if you had a program that read from a file, it should stop when it reads a Ctrl+Z, since that meant that it was now reading the padding.<\/p>\n<p>To retain compatibility with CP\/M, MS-DOS carried forward the Ctrl+Z convention. That way, when you transferred your files from your old CP\/M machine to your new PC, they wouldn&#8217;t have garbage at the end.<\/p>\n<p>Ctrl+Z hasn&#8217;t been needed for years; MS-DOS records file sizes in bytes rather than sectors. But the convention lingers in the &#8220;COPY&#8221; command, for example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Actually, text files don&#8217;t need to end in Ctrl+Z, but the convention persists in certain circles. (Though, fortunately, those circles are awfully small nowadays.) This story requires us to go back to CP\/M, the operating system that MS-DOS envisioned itself as a successor to. (Since the 8086 envisioned itself as the successor to the 8080, [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-40233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Actually, text files don&#8217;t need to end in Ctrl+Z, but the convention persists in certain circles. (Though, fortunately, those circles are awfully small nowadays.) This story requires us to go back to CP\/M, the operating system that MS-DOS envisioned itself as a successor to. (Since the 8086 envisioned itself as the successor to the 8080, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=40233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40233\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=40233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=40233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=40233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}