Re: StreamReader - ReadLine
From: Catalin Porancea (catalin.porancea_at_midwestern.net)
Date: 03/29/04
- Next message: Miguel Dias Moura: "C to VB. Can you please help me in solving this?"
- Previous message: Ken Hughes: "Passing LPBOOL values into / out of API functions"
- In reply to: Josh Moody [MSFT]: "RE: StreamReader - ReadLine"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 29 Mar 2004 09:21:56 -0600
Thanks a lot Josh. The file was indeed Unix and I was able to import it the
way I wanted it. Thanks again.
C
"Josh Moody [MSFT]" <Josh.Moody@online.microsoft.com> wrote in message
news:0lbOIpsEEHA.1160@cpmsftngxa06.phx.gbl...
> 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.
> --------------------
> >From: "Catalin Porancea" <catalin.porancea@midwestern.net>
> >Subject: StreamReader - ReadLine
> >Date: Fri, 19 Mar 2004 09:51:07 -0600
> >Lines: 32
> >X-Priority: 3
> >X-MSMail-Priority: Normal
> >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
> >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
> >Message-ID: <uPE7uocDEHA.3252@TK2MSFTNGP10.phx.gbl>
> >Newsgroups: microsoft.public.dotnet.languages.vb
> >NNTP-Posting-Host: host4.midwestern.net 209.107.68.4
> >Path:
>
cpmsftngxa06.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10
> phx.gbl
> >Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:189709
> >X-Tomcat-NG: microsoft.public.dotnet.languages.vb
> >
> >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
> >
> >
> >
>
- Next message: Miguel Dias Moura: "C to VB. Can you please help me in solving this?"
- Previous message: Ken Hughes: "Passing LPBOOL values into / out of API functions"
- In reply to: Josh Moody [MSFT]: "RE: StreamReader - ReadLine"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|