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
Solved

How to handle both linux and windows line endings

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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