Solved

How to handle both linux and windows line endings

Posted on 2013-06-13
4
218 Views
Last Modified: 2013-06-19
So the below code is what I use to process an ASCII file line by line.  It works great... UNLESS the file has been copied from a linux or apple machine, which has the different line endings (is it CR instead of CRLF?).

Can someone suggest a tweak to the code below to make it handle BOTH types of line endings?

Thanks very much...

Dim strm As New IO.StreamReader(sourcefilepath)
Dim txt As String = strm.ReadToEnd
Dim sr As New IO.StringReader(txt)
While Not sr.Peek < 0
   line_text = sr.ReadLine
' do something with line
End While

Open in new window

0
Comment
Question by:RonMexico
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 39246789
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39247276
How large are the files?
0
 

Author Comment

by:RonMexico
ID: 39247411
They are source code files, so could vary, you know, from 10-100000 lines.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39247422
I suspect they wouldn't be too large to load into memory. You could do something like:

Dim sourceCode As String = System.IO.File.ReadAllText(sourcefilepath)
Dim lines() As String = sourceCode.Split(new String() { vbCrLf + vbNewLine, vbNewLine, vbCrLf })

For Each line As String in lines
    ' do something with line
Next

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 53
Get id from json Data with NewtonSoft 3 32
consuming an asmx web service in winforms application 3 24
Connection String 16 43
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now