| | |
Problem in writing to a file using StreamWriter
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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)
Dim fs As New FileStream("file1.doc", FileMode.Create, FileAccess.Write) Dim s As New StreamWriter(fs) s.BaseStream.Seek(0, SeekOrigin.End) s.WriteLine(namebox.Text) s.WriteLine(scorebox.Text) s.Close()
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
0
#4 34 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...
•
•
Join Date: Aug 2009
Posts: 94
Reputation:
Solved Threads: 6
1
#5 34 Days Ago
You can add True to append on your code.
Notice that there is an additional boolean argument?. True if Append/Add.
You can use this new code, which also has a custom error.
Output to textfile or document file will look like this.
Player1 90
Player1 65
vb Syntax (Toggle Plain Text)
Dim s As New StreamWriter(fs,True)
You can use this new code, which also has a custom error.
vb Syntax (Toggle Plain Text)
Try Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True) Filewriter.WriteLine(namebox.Text & " " & scorebox.Text) Filewriter.Close() Catch E As Exception ' Your own custom error MsgBox("Cannot Save") End Try
Player1 90
Player1 65
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
0
#6 33 Days Ago
Using returns the below error.
Seems to be variable type mismatch (Bolean and string).
I used the following code:
VB.NET Syntax (Toggle Plain Text)
Dim s As New StreamWriter(fs,True)
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'.I used the following code:
VB.NET Syntax (Toggle Plain Text)
Dim fs As New FileStream("scores.txt", FileMode.Create, FileAccess.Write) Dim s As New System.IO.StreamWriter(fs, True) Try Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True) Filewriter.WriteLine(namebox.Text & " " & scorebox.Text) Filewriter.Close() Catch ex As Exception MsgBox("Cannot Save") End Try
0
#8 33 Days Ago
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)
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim fs As New FileStream("scores.txt", FileMode.Create, FileAccess.Write) Dim s As New System.IO.StreamWriter(fs) 'This gets the file create/append properties from the New Filestream() line Try Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True) Filewriter.WriteLine("abc" & " " & "123") Filewriter.Close() Catch ex As Exception MsgBox("Cannot Save") End Try End Sub
•
•
Join Date: Aug 2009
Posts: 94
Reputation:
Solved Threads: 6
0
#10 33 Days Ago
•
•
•
•
VB.NET Syntax (Toggle Plain Text)
Dim fs As New FileStream("scores.txt", FileMode.Create, FileAccess.Write) Dim s As New System.IO.StreamWriter(fs, True) Try Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True) Filewriter.WriteLine(namebox.Text & " " & scorebox.Text) Filewriter.Close() Catch ex As Exception MsgBox("Cannot Save") End Try
VB.NET Syntax (Toggle Plain Text)
Imports System.IO Private Sub Save() Try Dim Filewriter As StreamWriter = New StreamWriter("Scores.txt", True) Filewriter.WriteLine(namebox.Text & " " & scorebox.Text) Filewriter.Close() Catch ex As Exception MsgBox("Cannot Save") End Try End Sub
After that, you may proceed to checking the file if exists then create if exists.
Last edited by yorro; 33 Days Ago at 12:48 am.
![]() |
Similar Threads
- Problem with writing to a file (C++)
- Problem Writing in Text File (C++)
- Writing to File in Client/Server app (Java)
- writing to file (Python)
- Problem writing to file. (Java)
- Problem writing output file (C)
- Problem writing to file (C++)
- problem with dll file (Windows NT / 2000 / XP)
Other Threads in the VB.NET Forum
- Previous Thread: Child Form not Centering
- Next Thread: playbook application
| Thread Tools | Search this Thread |
.net .net2005 30minutes 2005 2008 access account arithmetic array basic binary bing button buttons c# center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy image images inline insert intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print problem problemwithinstallation project reports" save savedialog searchbox searchvb.net select serial server soap sql string table tcp text textbox timer toolbox trim update updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf






