Problem in writing to a file using StreamWriter

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 13
Reputation: Learning78 is an unknown quantity at this point 
Solved Threads: 0
Learning78 Learning78 is offline Offline
Newbie Poster

Problem in writing to a file using StreamWriter

 
0
  #1
32 Days Ago
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.
  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()
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 293
Reputation: TomW is on a distinguished road 
Solved Threads: 38
TomW TomW is offline Offline
Posting Whiz in Training
 
0
  #2
32 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 94
Reputation: yorro has a little shameless behaviour in the past 
Solved Threads: 6
yorro yorro is offline Offline
Junior Poster in Training
 
0
  #3
29 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Learning78 is an unknown quantity at this point 
Solved Threads: 0
Learning78 Learning78 is offline Offline
Newbie Poster
 
0
  #4
26 Days Ago
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 94
Reputation: yorro has a little shameless behaviour in the past 
Solved Threads: 6
yorro yorro is offline Offline
Junior Poster in Training
 
1
  #5
26 Days Ago
You can add True to append on your code.

  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Learning78 is an unknown quantity at this point 
Solved Threads: 0
Learning78 Learning78 is offline Offline
Newbie Poster
 
0
  #6
26 Days Ago
Using
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 62
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 15
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
0
  #7
26 Days Ago
Maybe a Filewriter.Flush() between each WriteLine()? Also, you might not need the Seek(), try taking that out and see if it works.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #8
25 Days Ago
There is no overload of StreamWriter(stream, append) because the "append" property is specified when you create the stream.

  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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 62
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 15
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
0
  #9
25 Days Ago
Is the code above correct? I see 2 StreamWriters created, and only one being used.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 94
Reputation: yorro has a little shameless behaviour in the past 
Solved Threads: 6
yorro yorro is offline Offline
Junior Poster in Training
 
0
  #10
25 Days Ago
Originally Posted by Learning78 View Post
  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.

  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; 25 Days Ago at 12:48 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC