Solved

How to handle both linux and windows line endings

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 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