-
Nov 11th, 2012, 02:22 PM
#1
Thread Starter
Fanatic Member
What is a line in a textfile?
Watching this post in another thread, I suddenly remembered once I tried something similar and got some empty lines. This prompted me to try an experiment:
Are the following equivalent:
* File.ReadAllLines(filename)
* File.ReadAllText(filename).Split(Environment.NewLine) '(Or ControlChars.CrLf - gives same result)
and does any of them actually split on Carriage Return followed by Linefeed and only there?
Surprisingly I found that the answer was no to both questions - which prompted me to question: what is a line in a textfile? I've allways assumed it had to end with CrLf.
File.ReadAllLines(filename) will split on Lf, Cr and CrLf (maybe more - haven't tested all controlchars)
File.ReadAllText(filename).Split(Environment.NewLine) seems to get confused and not act rationally at all!
File.ReadAllText(filename).Split() splits on whitespace causing CrLf to leave an undesired Lf string in the array.
File.ReadAllText(filename).Split(New Char() {ControlChars.Cr, ControlChars.Lf}) acts differently than all above.
I tested the examples and wrote my own version, of what I would expect for 'correct behaviour', with the following:
vb.net Code:
Dim filename As String = Path.Combine(Application.StartupPath, "Testing.txt")
File.WriteAllLines(filename, New String() {"Carriage" & ControlChars.Cr & "return", "Nothing", _
"Line" & ControlChars.Lf & "feed", "Spa ce"})
Dim lines() As String
Console.WriteLine("Version 1: ----------------------")
lines = File.ReadAllLines(filename)
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 2: ----------------------")
lines = File.ReadAllText(filename).Split(Environment.NewLine)
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 3: ----------------------")
lines = File.ReadAllText(filename).Split()
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 4: ----------------------")
lines = File.ReadAllText(filename).Split(New Char() {ControlChars.Cr, ControlChars.Lf})
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 5: ----------------------")
Dim total As String = File.ReadAllText(filename)
Dim i As Integer = 0
Dim length As Integer = 0
While i < total.Length
If total(i) = ControlChars.Lf Then
If i > 0 AndAlso total(i - 1) = ControlChars.Cr Then
Console.WriteLine(String.Format("'{0}'", If(i > 1, total.Substring(i - length, length - 1), String.Empty)))
length = 0
Else
length += 1
End If
Else
length += 1
End If
i += 1
End While
The question was: are lines in a textfile not supposed to be delimited by CrLf (for those who forgot it reading all that sheit)?.
Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Nov 11th, 2012, 02:30 PM
#2
Re: What is a line in a textfile?
it depends on how the file was saved.
textboxes use crlf + richtextboxes only use lf
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 11th, 2012, 03:26 PM
#3
Thread Starter
Fanatic Member
Re: What is a line in a textfile?
Originally Posted by .paul.
it depends on how the file was saved.
textboxes use crlf + richtextboxes only use lf
So there is no reason to maintain CrLf? And why was a new line ever implemented using 2 chars - old printers perhaps that had a different interpretation of Cr and Lf?
Anyways having 2 chars for a newline seems incredibly silly to me.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Nov 11th, 2012, 03:46 PM
#4
Re: What is a line in a textfile?
Cr and Lf are separate functions for printers. It was only on typewriters where both functions were integrated mechanically. But there's one very good reason to retain CrLf - the millions of text files written since the beginning of computing!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Nov 11th, 2012, 04:12 PM
#5
Thread Starter
Fanatic Member
Re: What is a line in a textfile?
Originally Posted by dunfiddlin
Cr and Lf are separate functions for printers. It was only on typewriters where both functions were integrated mechanically. But there's one very good reason to retain CrLf - the millions of text files written since the beginning of computing!
True! Tbh I didn't think about that. I guess changing formats and standards for files, hardware etc. is more difficult now than it has ever been.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|