StreamReader

  • Thread starter Thread starter Ian Oldbury
  • Start date Start date
I

Ian Oldbury

I'm having a problem reading from a flat file, in the file "£" and "»" exist
however when i view the contents of the variable LineContents these
characters don't exist.

Has anyone got any suggestions????

Dim LineContents as string

Dim fs As FileStream

Dim sr As StreamReader

fs = New FileStream(fp, FileMode.Open, FileAccess.Read, FileShare.Read)

sr = New StreamReader(fs)

LineContents = Convert.ToString(sr.ReadLine)
 
If the file is saved as UTFx, you may need to save it as ASCII.

Alternative, try this:

Dim reader as New StreamReader("yourfile.txt")
Dim yourLine as String = reader.ReadLine

There are other StreamReader constructors that allows you to handle the file
encoding.

Landers
 
Landley said:
If the file is saved as UTFx, you may need to save it as ASCII.

No, no, no. Neither '£' nor '»' are ASCII characters. The point is that
the original file was not UTF-8 encoded.
Alternative, try this:

Dim reader as New StreamReader("yourfile.txt")
Dim yourLine as String = reader.ReadLine

That's the default UTF-8 based StreamReader constructor, which doesn't
help at all. Nor is it much different from the OP's code. What do you
expect StreamReader to do with that file name under the hood? Yes,
create a FileStream ;-)
There are other StreamReader constructors that allows you to handle
the file encoding.

Yes, and these should be used. Encoding.Default might do the trick.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top