944,110 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3512
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 28th, 2009
0

Problem in writing to a file using StreamWriter

Expand Post »
Quote ...
Hi;
I have developed a MCQ based learning game. A user has to play the game repeatedly to improve his learning. For each session of game, player gets score. I want to write the score in a single file for every session. e.g. I want to print user name and his score for first game in first line of file and for 2nd game in second line and so on.
At the moment, every time a new session is played it overrides the earlier line. I need some help with the below code.
VB.NET Syntax (Toggle Plain Text)
  1. Dim fs As New FileStream("file1.doc", FileMode.Create, FileAccess.Write)
  2. Dim s As New StreamWriter(fs)
  3. s.BaseStream.Seek(0, SeekOrigin.End)
  4. s.WriteLine(namebox.Text)
  5. s.WriteLine(scorebox.Text)
  6. s.Close()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Learning78 is offline Offline
26 posts
since Sep 2009
Oct 28th, 2009
0
Re: Problem in writing to a file using StreamWriter
Take a look at "FileMode" in the help index. Your use of FileMode.Create will always create a new file including overwriting the file if it already exists. Instead you want to create a file if it doesnt exist but append to a file if it does exist.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Oct 31st, 2009
0
Re: Problem in writing to a file using StreamWriter
Let me first get this straight.

You want sorta like:
Game 1, User 100
Game 2, User 90


Instead you get this?
Game 2, User 90
Reputation Points: 8
Solved Threads: 7
Junior Poster
yorro is offline Offline
119 posts
since Aug 2009
Nov 3rd, 2009
0
Re: Problem in writing to a file using StreamWriter
Yeah, the second one overwrites the first. 
What I want is if first game score was 
Player1 90

Then second game score should not overwrite it rather appear alongwith it in file when I check file after playing game 2 e.g.

Player1 90
Player1 95 
Player1 96
and so on...
Reputation Points: 10
Solved Threads: 0
Light Poster
Learning78 is offline Offline
26 posts
since Sep 2009
Nov 3rd, 2009
1
Re: Problem in writing to a file using StreamWriter
You can add True to append on your code.

vb Syntax (Toggle Plain Text)
  1. Dim s As New StreamWriter(fs,True)
Notice that there is an additional boolean argument?. True if Append/Add.

You can use this new code, which also has a custom error.
vb Syntax (Toggle Plain Text)
  1. Try
  2. Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True)
  3. Filewriter.WriteLine(namebox.Text & " " & scorebox.Text)
  4. Filewriter.Close()
  5. Catch E As Exception
  6. ' Your own custom error
  7. MsgBox("Cannot Save")
  8. End Try
Output to textfile or document file will look like this.
Player1 90
Player1 65
Reputation Points: 8
Solved Threads: 7
Junior Poster
yorro is offline Offline
119 posts
since Aug 2009
Nov 3rd, 2009
0
Re: Problem in writing to a file using StreamWriter
Using
VB.NET Syntax (Toggle Plain Text)
  1. Dim s As New StreamWriter(fs,True)
returns the below error.
Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(path As String, append As Boolean)': Value of type 'System.IO.FileStream' cannot be converted to 'String'.
    'Public Sub New(stream As System.IO.Stream, encoding As System.Text.Encoding)': Value of type 'Boolean' cannot be converted to 'System.Text.Encoding'.
Seems to be variable type mismatch (Bolean and string).

I used the following code:
VB.NET Syntax (Toggle Plain Text)
  1. Dim fs As New FileStream("scores.txt", FileMode.Create, FileAccess.Write)
  2. Dim s As New System.IO.StreamWriter(fs, True)
  3. Try
  4. Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True)
  5. Filewriter.WriteLine(namebox.Text & " " & scorebox.Text)
  6. Filewriter.Close()
  7. Catch ex As Exception
  8. MsgBox("Cannot Save")
  9. End Try
Reputation Points: 10
Solved Threads: 0
Light Poster
Learning78 is offline Offline
26 posts
since Sep 2009
Nov 3rd, 2009
0
Re: Problem in writing to a file using StreamWriter
Maybe a Filewriter.Flush() between each WriteLine()? Also, you might not need the Seek(), try taking that out and see if it works.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Problem in writing to a file using StreamWriter
There is no overload of StreamWriter(stream, append) because the "append" property is specified when you create the stream.

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  2. Dim fs As New FileStream("scores.txt", FileMode.Create, FileAccess.Write)
  3. Dim s As New System.IO.StreamWriter(fs) 'This gets the file create/append properties from the New Filestream() line
  4. Try
  5. Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True)
  6. Filewriter.WriteLine("abc" & " " & "123")
  7. Filewriter.Close()
  8. Catch ex As Exception
  9. MsgBox("Cannot Save")
  10. End Try
  11. End Sub
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 3rd, 2009
0
Re: Problem in writing to a file using StreamWriter
Is the code above correct? I see 2 StreamWriters created, and only one being used.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 4th, 2009
0
Re: Problem in writing to a file using StreamWriter
Click to Expand / Collapse  Quote originally posted by Learning78 ...
VB.NET Syntax (Toggle Plain Text)
  1. Dim fs As New FileStream("scores.txt", FileMode.Create, FileAccess.Write)
  2. Dim s As New System.IO.StreamWriter(fs, True)
  3. Try
  4. Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True)
  5. Filewriter.WriteLine(namebox.Text & " " & scorebox.Text)
  6. Filewriter.Close()
  7. Catch ex As Exception
  8. MsgBox("Cannot Save")
  9. End Try
Ditch the first two lines, and create the .Txt manually first before you go into a more advance phase.

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.IO
  2. Private Sub Save()
  3. Try
  4. Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True)
  5. Filewriter.WriteLine(namebox.Text & " " & scorebox.Text)
  6. Filewriter.Close()
  7. Catch ex As Exception
  8. MsgBox("Cannot Save")
  9. End Try
  10. End Sub

After that, you may proceed to checking the file if exists then create if exists.
Last edited by yorro; Nov 4th, 2009 at 12:48 am.
Reputation Points: 8
Solved Threads: 7
Junior Poster
yorro is offline Offline
119 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Sorting Records using ReportViewer
Next Thread in VB.NET Forum Timeline: Windows Key Disable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC