This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Text file" – news ·newspapers ·books ·scholar ·JSTOR(December 2015) (Learn how and when to remove this message) |
| Text file | |
|---|---|
| Filename extension | .txt |
| Internet media type | text/plain |
| Type code | TEXT |
| Uniform Type Identifier (UTI) | public.plain-text |
| UTI conformation | public.text |
| Magic number | None |
| Type of format | Document file format,Generic container format |
Atext file (sometimes spelledtextfile; an old alternative name isflat file) is a kind ofcomputer file that is structured as a sequence oflines ofelectronic text. A text file existsstored as data within acomputer file system.
Inoperating systems such asCP/M, where the operating system does not keep track of the file size in bytes, the end of a text file is denoted by placing one or more special characters, known as anend-of-file (EOF) marker, as padding after the last line in a text file.[1] In modern operating systems such asDOS,Microsoft Windows andUnix-like systems, text files do not contain any special EOF character, because file systems on those operating systems keep track of the file size in bytes.[2]
Some operating systems, such asMultics, Unix-like systems, CP/M,DOS, theclassic Mac OS, and Windows, store text files as a sequence of bytes, with anend-of-linedelimiter at the end of each line. Other operating systems, such asOpenVMS andOS/360 and its successors, haverecord-oriented filesystems, in which text files are stored as a sequence either of fixed-length records or of variable-length records with a record-length value in the record header.
"Text file" refers to a type of container, whileplain text refers to a type of content.
At a generic level of description, there are two kinds of computer files: text files andbinary files.[3]
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(September 2024) (Learn how and when to remove this message) |

Because of their simplicity, text files are commonly used forstorage of information. They avoid some of the problems encountered with other file formats, such asendianness, padding bytes, or differences in the number of bytes in amachine word. Further, whendata corruption occurs in a text file, it is often easier to recover and continue processing the remaining contents. A disadvantage of text files is that they usually have a lowentropy, meaning that the information occupies more storage than is strictly necessary.
A simple text file may need no additionalmetadata (other than knowledge of itscharacter set) to assist the reader in interpretation. A text file may contain no data at all, which is a case ofzero-byte file.
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(September 2024) (Learn how and when to remove this message) |
TheASCII character set is the most common compatible subset of character sets for English-language text files, and is generally assumed to be the default file format in many situations. It covers American English, but for the Britishpound sign, theeuro sign, or characters used outside English, a richer character set must be used. In many systems, this is chosen based on the defaultlocale setting on the computer it is read on. Prior to UTF-8, this was traditionally single-byte encodings (such asISO-8859-1 throughISO-8859-16) for European languages andwide character encodings for Asian languages.
Because encodings necessarily have only a limited repertoire of characters, often very small, many are only usable to represent text in a limited subset of human languages.Unicode is an attempt to create a common standard for representing all known languages, and most known character sets are subsets of the very large Unicode character set. Although there are multiple character encodings available for Unicode, the most common isUTF-8, which has the advantage of being backwards-compatible with ASCII; that is, every ASCII text file is also a UTF-8 text file with identical meaning. UTF-8 also has the advantage thatit is easily auto-detectable. Thus, a common operating mode of UTF-8 capable software, when opening files of unknown encoding, is to try UTF-8 first and fall back to a locale dependent legacy encoding when it definitely is not UTF-8.
On most operating systems, the nametext file refers to a file format that allows onlyplain text content with very little formatting (e.g., nobold oritalic types). Such files can be viewed and edited ontext terminals or in simpletext editors. Text files usually have theMIME typetext/plain, usually with additional information indicating an encoding.

DOS andMicrosoft Windows use a common text file format, with each line of text separated by a two-character combination:carriage return (CR) andline feed (LF). It is common for the last line of textnot to be terminated with a CR-LF marker, and many text editors (includingNotepad) do not automatically insert one on the last line.
On Microsoft Windows operating systems, a file is regarded as a text file if the suffix of the name of the file (the "filename extension") is.txt. However, many other suffixes are used for text files with specific purposes. For example, source code for computer programs is usually kept in text files that have file name suffixes indicating theprogramming language in which the source is written.
Most Microsoft Windows text files use ANSI, OEM, Unicode or UTF-8 encoding. What Microsoft Windows terminology calls "ANSI encodings" are usually single-byteISO/IEC 8859 encodings (i.e. ANSI in the Microsoft Notepad menus is really "System Code Page", non-Unicode, legacy encoding), except for in locales such as Chinese, Japanese and Korean that require double-byte character sets. ANSI encodings were traditionally used as default system locales within Microsoft Windows, before the transition to Unicode. By contrast, OEM encodings, also known asDOS code pages, were defined byIBM for use in the originalIBM PC text mode display system. They typically include graphical andline-drawing characters common in DOS applications. "Unicode"-encoded Microsoft Windows text files contain text inUTF-16 Unicode Transformation Format. Such files normally begin withbyte order mark (BOM), which communicates the endianness of the file content. Although UTF-8 does not suffer from endianness problems, many Microsoft Windows programs (i.e. Notepad) prepend the contents of UTF-8-encoded files with BOM,[4] to differentiate UTF-8 encoding from other 8-bit encodings.[5]
OnUnix-like operating systems, text files format is precisely described:POSIX defines a text file as a file that contains characters organized into zero or more lines,[6] where lines are sequences of zero or more non-newline characters plus a terminating newline character,[7] normally LF.
Additionally, POSIX defines aprintable file as a text file whose characters are printable or space or backspace according to regional rules. This excludes most control characters, which are not printable.[8]
Prior to the advent ofmacOS, theclassic Mac OS system regarded the content of a file (the data fork) to be a text file when itsresource fork indicated that the type of the file was "TEXT".[9] Lines of classic Mac OS text files are terminated with CR characters.[10]
Being a Unix-like system, macOS uses Unix format for text files.[10]Uniform Type Identifier (UTI) used for text files in macOS is "public.plain-text"; additional, more specific UTIs are: "public.utf8-plain-text" for utf-8-encoded text, "public.utf16-external-plain-text" and "public.utf16-plain-text" for utf-16-encoded text and "com.apple.traditional-mac-plain-text" for classic Mac OS text files.[9]
When opened by a text editor, human-readable content is presented to the user. This often consists of the file's plain text visible to the user. Depending on the application, control codes may be rendered either as literal instructions acted upon by the editor, or as visibleescape characters that can be edited as plain text. Though there may be plain text in a text file, control characters within the file (especially the end-of-file character) can render the plain text unseen by a particular method.
The use oflightweight markup languages such asTeX,markdown andwikitext can be regarded as an extension of plain text files, as marked-up text is still wholly or partially human-readable in spite of containing machine-interpretable annotations. Early uses ofHTML could also be regarded in this way, although the HTML of modern websites is largely unreadable by humans. Other file formats such asenriched text andCSV can also be regarded as human-interpretable to some degree.
Yes, UTF-8 can contain a BOM. However, it makesno difference as to the endianness of the byte stream. UTF-8 always has the same byte order. An initial BOM is only used as a signature — an indication that an otherwise unmarked text file is in UTF-8. Note that some recipients of UTF-8 encoded data do not expect a BOM. Where UTF-8 is usedtransparently in 8-bit environments, the use of a BOM will interfere with any protocol or file format that expects specific ASCII characters at the beginning, such as the use of "#!" of at the beginning of Unix shell scripts.