StreamReader - ReadLine

  • Thread starter Thread starter Catalin Porancea
  • Start date Start date
C

Catalin Porancea

Hello,

I want to read a file that has several thousands lines, line by line and
load every line in a string. The problem is that the file is created on a
Unix machine which doesn't and the line with a carriage return (hexadecimal
0x000d) or a line feed (hexadecimal 0x000a). If I open the file with
Notepad, the file looks scrambled and where the line was supposed to end
there is a strange character that looks like a square and I cannot even copy
it. Because of that the ReadLine doesn't stop where it is supposed to and
keeps reading beyond that character.

____________________________________________________________________________
___
Dim fsr As StreamReader = File.OpenText(strPath)
Dim s As String
While fsr.Peek <> -1
s = fsr.ReadLine
cmd.CommandText = "insert into import_temp (record,fileid) values ('" & s &
"','" & strImport_FileID & "')"
Try
cmd.ExecuteNonQuery()
Catch Ex As Exception
lblMSG.Text = "Error: " & Ex.Message.ToString()
Finally
End Try
____________________________________________________________________________
___
Can anybody help with this?
Thank you,
Catalin
 
Are you saying it doesn't end in a CrLF or it doesn't end in a Cr or Lf?

I have some code which can probably help you; if it doesn't end in a Cr or
an Lf, you should be able to figure out what it ends with, and use that.
(This is part of a function that I wrote; it should be enough to help you
out.)

Enum enuFileSystem
Unix
Windows
Mac
End Enum

Dim line As String
Dim oldpos As Integer
Dim pos As Integer = 1
Dim s As String = sr.ReadToEnd
If your stream is large, loading it all into a string is probably a bad
idea; you may want to break this up into reading blocks of text and
searching for the "magic" character

'determine filesystem on the fly now. :)
If Not blnDontAutoDetectFileType Then
If InStr(pos + 1, s, vbCrLf) > 0 Then
enuFileSys = enuFileSystem.Windows
ElseIf InStr(pos + 1, s, vbCr) > 0 Then
enuFileSys = enuFileSystem.Mac
ElseIf InStr(pos + 1, s, vbLf) > 0 Then
enuFileSys = enuFileSystem.Unix
Else
'just use whatever they passed in, cause i can't figure
it out....
End If
End If

While True
oldpos = pos
If enuFileSys = enuFileSystem.Unix Then
pos = InStr(pos + 1, s, vbLf) 'unix uses line-feed, not
crlf
ElseIf enuFileSys = enuFileSystem.Mac Then
pos = InStr(pos + 1, s, vbCr) 'Not sure if Mac's
actually do this, but i have seen some files turn up this way, so let's
just call it a mac :)
Else
pos = InStr(pos + 1, s, vbCrLf)
End If
If pos = 0 Then Exit While
line = IIf(oldpos = 1, Mid(s, oldpos, pos - oldpos), Mid(s,
oldpos + 1, pos - oldpos - 1))
' here you've got your line; you should be able to muck with it as
necessary.
End While

Josh Moody
Developer Division Sustained Engineering Team

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
 
Thanks a lot Josh. The file was indeed Unix and I was able to import it the
way I wanted it. Thanks again.

C
 
Back
Top