Solved

How to handle both linux and windows line endings

Posted on 2013-06-13
4
217 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 82

Accepted Solution

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

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

21 Experts available now in Live!

Get 1:1 Help Now