Results 1 to 21 of 21

Thread:  Character in a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

     Character in a text file

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re:  Character in a text file

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

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

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    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...
    Code:
    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...
    Last edited by gigemboy; Feb 6th, 2006 at 05:23 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

    thanks. figured out a way to do it

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re:  Character in a text file

    Quote Originally Posted by eben
    thanks. figured out a way to do it
    Care to share?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re:  Character in a text file

    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:
    VB Code:
    1. myStreamReader = File.OpenText(FileName)
    which uses UTF-8 encoding, try this:
    VB Code:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re:  Character in a text file

    Quote Originally Posted by eben
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

    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

  12. #12
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    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...
    Last edited by gigemboy; Feb 7th, 2006 at 04:53 AM.

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

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

  14. #14
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    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...

  15. #15

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

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

  16. #16
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    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...
    VB Code:
    1. Dim myreader As New IO.StreamReader("c:\AACD1.txt")
    2.         While myreader.Peek <> -1
    3.             MessageBox.Show(myreader.ReadLine) 'displayed the full line, with the character
    4.         End While
    5.         MyReader.Close()
    Last edited by gigemboy; Feb 7th, 2006 at 05:49 AM.

  17. #17
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    Was even able to split the line on the "2" ascii character fine... so all should be fine on the streamreader side...
    VB Code:
    1. Dim myreader As New IO.StreamReader("c:\AACD1.txt")
    2.         While myreader.Peek <> -1
    3.             Dim MyStrings() As String = myreader.ReadLine.Split(Chr(2))
    4.             For Each str As String In MyStrings
    5.                 MessageBox.Show(str) 'displays 2 messageboxes, with the split strings
    6.             Next
    7.         End While
    8.         MyReader.Close()

  18. #18

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

    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.

  19. #19
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    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...

  20. #20

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    10

    Re:  Character in a text file

    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

  21. #21
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re:  Character in a text file

    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width