Results 1 to 9 of 9

Thread: [RESOLVED] Stream i/o issue

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    41

    Resolved [RESOLVED] Stream i/o issue

    Hi,

    Just starting up on VB 2008 Express (.NET).
    Trying to write to and read from a file.
    It runs without errors, but the messagebox only shows the 'OK' button, not the contents of the file as I want. I thought that maybe I have to rewind the file before reading, but AFAICS there is no 'StreamRead.Rewind' or Reset or something.
    What am I missing, please?

    Code:
    Imports System.IO
    
    Public Class ArrayTest
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim strId As String
            Dim intPeriod As Int16
            Dim intValue As Int16
    
            Dim DataFileName As New String("C:\Documents and Settings\Janneman\Bureaublad\bTFiletest.txt")
            Dim objDataFile As New FileStream(DataFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
            Dim objDataWriter As New StreamWriter(objDataFile)
    
            strId = "Bias"
            intPeriod = 25
            intValue = 35
    
            objDataWriter.Write(strId & CStr(intPeriod) & CStr(intValue))
    
            If File.Exists(DataFileName) Then
                Dim objDataReader As New StreamReader(objDataFile)
                MessageBox.Show(objDataReader.ReadToEnd)
            Else : MessageBox.Show("File doesn't exist")
            End If
    
            objDataWriter.Close()
            objDataFile.Close()
    
    
        End Sub
    End Class
    Jan Didden

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Stream i/o issue

    I didnt test it but I think this will work;

    Code:
            Dim strId As String = "Bias"
            Dim intPeriod As Int16 = 25
            Dim intValue As Int16 = 35
            Dim DataFileName As String = "C:\Documents and Settings\Janneman\Bureaublad\bTFiletest.txt"
    
            Dim objDataWriter As New System.IO.StreamWriter(DataFileName)
            objDataWriter.Write(strId & CStr(intPeriod) & CStr(intValue))
            objDataWriter.Close()
    
            If System.IO.File.Exists(DataFileName) Then
                Dim objDataReader As New System.IO.StreamReader(DataFileName)
                MessageBox.Show(objDataReader.ReadToEnd)
                objDataReader.Close()
            Else
                MessageBox.Show("File doesn't exist")
            End If

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    41

    Re: Stream i/o issue

    Hi Bulldog,

    Thanks, but unfortunately, no change. Still only the 'OK' button on the textbox, not the file contents....

    Edit: the file write works, the three entries end up in the correct filename.

    Jan Didden
    Last edited by janneman; Feb 26th, 2009 at 07:22 AM.

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Stream i/o issue

    Strange, I just tested it myself (the code I posted) and it worked ok.

    Check that your filename is actually created and that it has the correct contents. In the file I see the text as "Bias2535".

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    41

    Re: Stream i/o issue

    Yes the file contents is OK.
    Strange. Maybe I should try to write to a listbox for instance?

    Jan Didden

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    41

    Re: Stream i/o issue

    An admin thing: how can I copy code from a post into my project? Highlight|Ctrl-C|Ctrl-V doesn't seem to work.

    Jan Didden

  7. #7
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Stream i/o issue

    Try changing this;
    Code:
                Dim objDataReader As New System.IO.StreamReader(DataFileName)
                MessageBox.Show(objDataReader.ReadToEnd)
                objDataReader.Close()
    to this;
    Code:
                Dim objDataReader As New System.IO.StreamReader(DataFileName)
                Dim Result As String = objDataReader.ReadToEnd
                objDataReader.Close()
                MessageBox.Show(Result)

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    41

    Re: Stream i/o issue

    Yes that does it! But I have to close the DataWriter stream before doing the DataRead, otherwise I get a runtime exception.

    Thanks a bunch, now it's onward to reading/writing CSV-files for Exell....

    Have a nice day,

    Jan Didden

  9. #9
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Stream i/o issue

    Please mark the thread resolved if you require no further assistance.

    Glad to see your problem was solved. Welcome to the forums!
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

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