DotNet Visual Basic: Character in a text file

  • eben / 104 / Wed, 24 Jun 2009 12:41:00 GMT / Comments (20)
  • o'm reading a text file into a textbox. problem is the character appears in the first line of text and streamreader does not read past this character. i need a way to open the text file, replace all similar characters and then save the text file. pls help
  • Keywords:

    character, text, file, dotnet, visual basic, vb, .net

  • http://dotnet.itags.org/dotnet-visual-basic/182930/«« Last Thread - Next Thread »»
    1. Is this actually a text file? Are you sure that it's not a binary file? If it is a text file then how was it written in the first place? You may need to specify the encoding when you create the StreamReader.

      jmcilhinney | Tues, 04 Dec 2007 14:23:00 GMT |

    2. the file has an .str extension, but opens fine in notepad. it is a database that i need to eventually import into SQL.

      eben | Tues, 04 Dec 2007 14:24:00 GMT |

    3. It's just a whitespace character or special character that notepad displays as a "box", just have to find out what that character is... read the line in and display each ascii value... here is a sample on how to do it...

      For Each Letter As Char In MyString
      Messagebox.Show(" """ & Letter & """=" & Asc(Letter))
      Next

      Quotes are used so you can see the "space" that will show when it gets to the special character...

      gigemboy | Tues, 04 Dec 2007 14:25:00 GMT |

    4. thanks. figured out a way to do it

      eben | Tues, 04 Dec 2007 14:26:00 GMT |

    5. thanks. figured out a way to do itCare to share?

      jmcilhinney | Tues, 04 Dec 2007 14:27:00 GMT |

    6. here's the code:

      Dim myStreamReader As StreamReader
      Dim myNextInt As Integer
      Dim FileName As String
      Try
      If TxtImportFile.Text = "" Then
      MsgBox("No file selected", MsgBoxStyle.Information)
      Else
      FileName = TxtImportFile.Text
      myStreamReader = File.OpenText(FileName)
      TxtData.Clear()

      myNextInt = myStreamReader.Read()

      While myNextInt <> -1

      TxtData.Text += ChrW(myNextInt)
      myNextInt = myStreamReader.Read()
      TxtData.Refresh()
      End While
      End If
      Catch exc As Exception
      Finally
      If Not myStreamReader Is Nothing Then
      myStreamReader.Close()
      End If
      End Try

      it seems like the ChrW did the trick. i'm new to vb so i hope it wasn't the obvious thing to do, it took me a while before i figured it out.

      eben | Tues, 04 Dec 2007 14:28:00 GMT |

    7. That's certainly not a very efficient way to read a text file. I'm quite sure that if you specified a different encoding when opening the StreamReader it would read OK. Instead of this:myStreamReader = File.OpenText(FileName)which uses UTF-8 encoding, try this:mySreamReader = New IO.StreamReader(FileName, System.Text.Encoding.Unicode)If that doesn't work then you can use each of the other Encoding values to see if they work. That way you can use ReadLine or ReadToEnd to read your file rather than one character at a time.

      jmcilhinney | Tues, 04 Dec 2007 14:29:00 GMT |

    8. i tried ASCII, UNICODE, UTF7 and UTF8 encodings, but none of them read past the  character.

      eben | Tues, 04 Dec 2007 14:30:00 GMT |

    9. i tried ASCII, UNICODE, UTF7 and UTF8 encodings, but none of them read past the  character.Bizarre. Out of interest, whats the integer value that you read?

      jmcilhinney | Tues, 04 Dec 2007 14:31:00 GMT |

    10. If i read it correctly it seems to be a 2. don't waste too much of your time on this. thanks alot for your help so far

      eben | Tues, 04 Dec 2007 14:32:00 GMT |

    11. Hmm.. well if it was an Ascii "2", that would be a "^B" or "STX" character... STX meaning "Start Transmission"... very odd character to have in a text file...

      gigemboy | Tues, 04 Dec 2007 14:33:00 GMT |

    12. would that have affected streamreader's readline or read methods?

      eben | Tues, 04 Dec 2007 14:34:00 GMT |

    13. I dont think so... should still read it...

      How about uploading the text file, or a line in the textfile that contains the character? There is an attachments section when you post that allows you to upload files...

      gigemboy | Tues, 04 Dec 2007 14:35:00 GMT |

    14. i uploaded the file. its AACD1.txt. it has a .str extension though.

      eben | Tues, 04 Dec 2007 14:36:00 GMT |

    15. Well it does seem to be a ASCII "2" value... but It didn't have any problems reading in the line... displayed all the text...

      Dim myreader As New IO.StreamReader("c:\AACD1.txt")
      While myreader.Peek <> -1
      MessageBox.Show(myreader.ReadLine) 'displayed the full line, with the character
      End While
      MyReader.Close()

      gigemboy | Tues, 04 Dec 2007 14:37:00 GMT |

    16. Was even able to split the line on the "2" ascii character fine... so all should be fine on the streamreader side...

      Dim myreader As New IO.StreamReader("c:\AACD1.txt")
      While myreader.Peek <> -1
      Dim MyStrings() As String = myreader.ReadLine.Split(Chr(2))
      For Each str As String In MyStrings
      MessageBox.Show(str) 'displays 2 messageboxes, with the split strings
      Next
      End While
      MyReader.Close()

      gigemboy | Tues, 04 Dec 2007 14:38:00 GMT |

    17. can you try doing the same with the file as an .str extension? i've got the 101 VB.NET samples, and their text reading program can also only read the file by characters. ReadLine and ReadToEnd does not seem to work.

      eben | Tues, 04 Dec 2007 14:39:00 GMT |

    18. The extension should be of no consequence, as the text will still be the same... unless the .STR file you have was another type of encoding, and it changed it when you opened and saved the line of text in notepad... On my end, without testing the actual STR file, simply changing the extension to ".STR" will have no effect, it would still read...

      gigemboy | Tues, 04 Dec 2007 14:40:00 GMT |

    19. it seems like just renaming the file to a .txt type works, but the original .STR does not want to work with ReadLine or ReadToEnd. i tried all the encodings. unfortunately i have to write the program so it opens files of any type of extensions and i need to make sure it reads to the end.

      thanks for your help

      eben | Tues, 04 Dec 2007 14:41:00 GMT |

    20. Have you tried just pasting my code in (from my first streamreader post), and read in the .STR file (just change "AACD1.txt" to "AACD1.STR")? It should loop for all lines in the file and display each line in a messagebox... the extension should have nothing to do with it...

      gigemboy | Tues, 04 Dec 2007 14:42:00 GMT |

  • DotNet Visual Basic Questions

    • Using a .NET DLL in SQL

      Hi,I've heard that in VB.NET you can create DLL's that allow you to makesystem-level functio...

      By scottmcnair, 1 Comments

    • Reading dll-Functions and execute them

      Hi everybodyI am trying to load an assembly (dll) an this works fine for me with:Dim SampleAssembly ...

      By halimajinijazi, 2 Comments

    • Edit Box question

      Hello,Is there a way to have access to the text buffer of an Edit box from ithwnd. I can use SetWind...

      By sam, 1 Comments

    • Reading EBCDIC Files.

      I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is...

      By jeffmvia_net247, 9 Comments

    • help me....

      im currently working on a .dll, can somebody give me an idea on how can i usesome of my .exe class.....

      By sheryllviadotnetmonster_com, 2 Comments

    • Reading EBCDIC Files.

      I'm still having problems reading EBCDIC files=2E Currently it=looks like the lower range (0 to ...

      By jeffmvia_net247, 9 Comments

    • Reading each line in a variable

      Hi,I want to read line by line in a variable, but it could not be foward only , it need to be forwar...

      By ltt19, 6 Comments

    • edit box

      how can i read a .csv file and output it into an edit box?i also need to know how can i display more...

      By wayne, 1 Comments