|
-
Feb 26th, 2009, 06:54 AM
#1
Thread Starter
Member
[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
-
Feb 26th, 2009, 07:06 AM
#2
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
-
Feb 26th, 2009, 07:18 AM
#3
Thread Starter
Member
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.
-
Feb 26th, 2009, 07:20 AM
#4
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".
-
Feb 26th, 2009, 07:25 AM
#5
Thread Starter
Member
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
-
Feb 26th, 2009, 07:28 AM
#6
Thread Starter
Member
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
-
Feb 26th, 2009, 07:30 AM
#7
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)
-
Feb 26th, 2009, 08:18 AM
#8
Thread Starter
Member
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
-
Feb 26th, 2009, 09:54 AM
#9
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|