?
Solved

How to handle both linux and windows line endings

Posted on 2013-06-13
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 1000 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 1000 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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