-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Document trailing newline behavior for ReadLine and ReadAllLines/ReadLines APIs #12493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e8782f8
c2b9135
19dd695
06c0a66
8145f7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4846,6 +4846,8 @@ The following example moves a file. | |||||
| ## Remarks | ||||||
| This method opens a file, reads each line of the file, then adds each line as an element of a string array. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed. | ||||||
|
|
||||||
| If the file ends with a newline sequence, no additional empty line is appended to the array. For example, a file containing `"line1\nline2\n"` produces the same two-element array (`["line1", "line2"]`) as a file containing `"line1\nline2"`. | ||||||
|
|
||||||
| This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected. | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -4945,6 +4947,8 @@ The following example moves a file. | |||||
| ## Remarks | ||||||
| This method opens a file, reads each line of the file, and then adds each line as an element of a string array. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed. | ||||||
|
|
||||||
| If the file ends with a newline sequence, no additional empty line is appended to the array. For example, a file containing `"line1\nline2\n"` produces the same two-element array (`["line1", "line2"]`) as a file containing `"line1\nline2"`. | ||||||
|
|
||||||
| This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected. | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -5460,8 +5464,11 @@ You can use the <xref:System.IO.File.ReadLines*> method to do the following: | |||||
|
|
||||||
| This method uses <xref:System.Text.Encoding.UTF8*> for the encoding value. | ||||||
|
|
||||||
| A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. If the file ends with a newline sequence, no additional empty line is returned. For example, a file containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as a file containing `"line1\nline2"`. | ||||||
|
|
||||||
| ## Examples | ||||||
| The following example reads the lines of a file to find lines that contain specified strings. | ||||||
|
|
||||||
| The following example reads the lines of a file to find lines that contain specified strings. | ||||||
|
|
||||||
| :::code language="csharp" source="~/snippets/csharp/System.IO/File/ReadLines/program.cs" id="Snippet1"::: | ||||||
| :::code language="fsharp" source="~/snippets/fsharp/System.IO/File/ReadLines/program.fs" id="Snippet1"::: | ||||||
|
|
@@ -5568,6 +5575,8 @@ You can use the <xref:System.IO.File.ReadLines*> method to do the following: | |||||
| - Write the returned collection of lines to a file with the <xref:System.IO.File.WriteAllLines(System.String,System.Collections.Generic.IEnumerable{System.String},System.Text.Encoding)?displayProperty=nameWithType> method, or append them to an existing file with the <xref:System.IO.File.AppendAllLines(System.String,System.Collections.Generic.IEnumerable{System.String},System.Text.Encoding)?displayProperty=nameWithType> method. | ||||||
| - Create an immediately populated instance of a collection that takes an <xref:System.Collections.Generic.IEnumerable`1> collection of strings for its constructor, such as a <xref:System.Collections.Generic.IList`1> or a <xref:System.Collections.Generic.Queue`1>. | ||||||
|
|
||||||
| A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. If the file ends with a newline sequence, no additional empty line is returned. For example, a file containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as a file containing `"line1\nline2"`. | ||||||
|
||||||
| A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. If the file ends with a newline sequence, no additional empty line is returned. For example, a file containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as a file containing `"line1\nline2"`. | |
| A line is defined as a sequence of characters terminated by a carriage return ('\r'), a line feed ('\n'), a carriage return immediately followed by a line feed, or the end of the file or stream. If the file ends with a newline sequence, no additional empty line is returned. For example, a file containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as a file containing `"line1\nline2"`. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1195,6 +1195,8 @@ | |||||||||||||||||||||
| ## Remarks | ||||||||||||||||||||||
| A line is defined as a sequence of characters followed by a carriage return (0x000d), a line feed (0x000a), a carriage return followed by a line feed, <xref:System.Environment.NewLine*?displayProperty=nameWithType>, or the end-of-stream marker. The string that is returned does not contain the terminating carriage return or line feed. The return value is `null` if the end of the input stream has been reached. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| If the stream ends with a newline sequence, no additional empty line is returned. For example, a stream containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as a stream containing `"line1\nline2"`. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| If the method throws an <xref:System.OutOfMemoryException> exception, the reader's position in the underlying <xref:System.IO.Stream> is advanced by the number of characters the method was able to read, but the characters that were already read into the internal <xref:System.IO.TextReader.ReadLine*> buffer are discarded. Because the position of the reader in the stream cannot be changed, the characters that were already read are unrecoverable and can be accessed only by reinitializing the <xref:System.IO.TextReader> object. If the initial position within the stream is unknown or the stream does not support seeking, the underlying <xref:System.IO.Stream> also needs to be reinitialized. | ||||||||||||||||||||||
|
Comment on lines
1196
to
1200
|
||||||||||||||||||||||
| A line is defined as a sequence of characters followed by a carriage return (0x000d), a line feed (0x000a), a carriage return followed by a line feed, <xref:System.Environment.NewLine*?displayProperty=nameWithType>, or the end-of-stream marker. The string that is returned does not contain the terminating carriage return or line feed. The return value is `null` if the end of the input stream has been reached. | |
| If the stream ends with a newline sequence, no additional empty line is returned. For example, a stream containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as a stream containing `"line1\nline2"`. | |
| If the method throws an <xref:System.OutOfMemoryException> exception, the reader's position in the underlying <xref:System.IO.Stream> is advanced by the number of characters the method was able to read, but the characters that were already read into the internal <xref:System.IO.TextReader.ReadLine*> buffer are discarded. Because the position of the reader in the stream cannot be changed, the characters that were already read are unrecoverable and can be accessed only by reinitializing the <xref:System.IO.TextReader> object. If the initial position within the stream is unknown or the stream does not support seeking, the underlying <xref:System.IO.Stream> also needs to be reinitialized. | |
| A line is defined as a sequence of characters followed by a carriage return (0x000d), a line feed (0x000a), a carriage return followed by a line feed, <xref:System.Environment.NewLine*?displayProperty=nameWithType>, or the end of the reader's input. The string that is returned does not contain the terminating carriage return or line feed. The return value is `null` if the end of the input has been reached. | |
| If the input ends with a newline sequence, no additional empty line is returned. For example, input containing `"line1\nline2\n"` produces the same two lines (`"line1"` and `"line2"`) as input containing `"line1\nline2"`. | |
| If the method throws an <xref:System.OutOfMemoryException> exception, the reader's position in its underlying source is advanced by the number of characters the method was able to read, but the characters that were already read into the internal <xref:System.IO.TextReader.ReadLine*> buffer are discarded. Because the reader's position in the underlying source cannot be reset, the characters that were already read are unrecoverable and can be accessed only by reinitializing the <xref:System.IO.TextReader> object. If the initial position within the underlying source is unknown or the source does not support repositioning, the underlying source also needs to be reinitialized. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new line definition implies that a line is always terminated by a CR/LF sequence, but
File.ReadLinesalso returns the final line when the file ends without a trailing newline. Consider updating the definition to include end-of-file/end-of-stream as a valid line terminator.