-
Dec 17th, 2017, 08:35 AM
#1
Thread Starter
New Member
Saving to text file
Hello,
I'm trying to save whether the user got the answers to questions "right" or "wrong" in a list to a text file. When I try to do this, it only ends up saving one line of an answer. My code is below:
Dim File As IO.StreamWriter
Dim Answer As String
For i = 0 To 10
If AnswerCheck(i) = True Then
Answer = "Right"
Else
Answer = "Wrong"
End If
File = New IO.StreamWriter("Answers.txt")
File.WriteLine(Answer)
File.Close()
Next
Thank you.
-
Dec 17th, 2017, 09:30 AM
#2
Re: Saving to text file
If you expect Answer to contain multiple instances of "Right" and/or "Wrong" then you have to add multiple instances to it. Let's say that you had an Integer variable 'x' and you assign 10 to it. If you wanted to add 5 to it so that it then equaled 15, would you do this?
Of course you wouldn't, because that's not adding 5 to the existing value but rather replacing the existing value with 5. Now look at your code and ask yourself whether it does what you intend. Better yet, debug your code and watch it in action. If you don't know how to debug, now is the time to learn. Set a breakpoint on the first line and then step through you code and watch the state as you go and see if it actually does what you intend.
By the way, please use the [CODE] button on the tool bar to wrap your code snippets in formatting tags for readability.
-
Dec 17th, 2017, 10:38 AM
#3
Re: Saving to text file
Originally Posted by RandomAccount11
Hello,
I'm trying to save whether the user got the answers to questions "right" or "wrong" in a list to a text file. When I try to do this, it only ends up saving one line of an answer. My code is below:
Code:
Dim File As IO.StreamWriter
Dim Answer As String
File = New IO.StreamWriter("Answers.txt")
For i = 0 To 10
If AnswerCheck(i) = True Then
Answer = "Right"
Else
Answer = "Wrong"
End If
File.WriteLine(Answer)
Next
File.Close()
Thank you.
I'm pretty sure the line File = New IO.StreamWriter("Answers.txt") is generating a brand new file each time through the For loop. So I would expect you are seeing only the result of AnswerCheck(10) present in the file once your code completes.
If you are doing repeated file I/O like this in a loop, you are much better off in general placing the code to open and close the file outside of the loop. So the File = New IO.StreamWriter("Answers.txt") line should be just above the start of your For loop, and the File.Close() line should be just after the Next statement (see above)
-
Dec 17th, 2017, 01:01 PM
#4
Thread Starter
New Member
Re: Saving to text file
Thank you. It's now working perfectly.
-
Dec 17th, 2017, 05:48 PM
#5
Re: Saving to text file
I realise now that I misread your code, and that was in part because you didn't use code tags and the code wasn't indented properly. With the indenting shown in post #3, I see what I missed the first time. That's a perfect example of why formatting code snippets is important.
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
|