[RESOLVED] Writing Information Onto Text File
I am in the final stages of my Card Sharks game development, and I am wondering: How do I carry the champion's name and his/her corresponding winnings amount onto a text file?
1. The Champion's name is denoted by "gstrChampName".
2. The Champion's accumulated winnings from previous games is denoted by "PrevWinnings".
How do I write their values onto a text file so that the text file can act as a database?
Re: Writing Information Onto Text File
Search the forum for "write text file", there are many code samples for it.
EDIT: Here's one such similar thread http://www.vbforums.com/showthread.p...ght=text+score
Re: Writing Information Onto Text File
I think I am on the right track, as I did a sample file with a single word in a text file.
Now, I am learning how to create an array where there are two items in a text file separated by a line separator.
But I seem to have a type mismatch error with regards to the RedChampName. The PrevWinnings is okay, as I hovered the mouse over the PrevWinnings area. But when I hover the mouse over the RedChampName, I get (RedChampName = ""). How am I supposed to fix this?
Here is the code I have so far:
Code:
Open (App.Path & "\ReturnChamp.txt") For Input As 1
Line Input #1, ChampTmpstr
ChampTmpstr = Split(ChampTmpstr, "|")
RedChampName = tmpstr: PrevWinnings = tmplng
Close
Did I do anything wrong here?
Re: Writing Information Onto Text File
Quote:
Did I do anything wrong here?
You missed #
Code:
Open (App.Path & "\ReturnChamp.txt") For Input As #1
Line Input #1, ChampTmpstr
ChampTmpstr = Split(ChampTmpstr, "|")
RedChampName = tmpstr: PrevWinnings = tmplng
Close
Putting bracket is optional.
I have no idea about SPLIT function you've used.
Re: Writing Information Onto Text File
Actual, rajbdilip, the # is optional in the Open and Close statements.
The problem is there's no assignment being done to either tmpstr or tmplng.
These should be replaced or assigned with a string array, which is assigned from the return of the Split function.
vb Code:
Dim saScore() as String
Open (App.Path & "\ReturnChamp.txt") For Input As 1
Do While Not EOF(1)
Line Input #1, ChampTmpstr
If instr(ChampTmpstr,"|") then
saScore = Split(ChampTmpstr, "|")
tmpStr = saScore(0): tmpLng = CLng(saScore(1))
RedChampName = tmpstr: PrevWinnings = tmplng
Else
Msgbox "Error Parsing Scores!"
End If
Loop
Close 1
You both missed the file number of the Close statement, though. Unless the intention is to close all open files.
Re: Writing Information Onto Text File
This would be an better idea to retrieve the scores from your database file you say.
Code:
Dim Names() as String, Scores() as Integer
Static Counter as Integer
While Not EOF(1)
Counter=Counter + 1
ReDim Preserve Names(Counter)
ReDim Preserve Scores(Counter)
Open App.Path & "\ReturnChamp.txt" For Input As #1
Input #1, Names(Counter), Scores(Counter)
Close #1
Wend
Re: Writing Information Onto Text File
Quote:
Originally Posted by
rajbdilip
This would be an better idea to retrieve the scores from your database file you say.
Code:
Dim Names() as String, Scores() as Integer
Static Counter as Integer
While Not EOF(1)
Counter=Counter + 1
ReDim Preserve Names(Counter)
ReDim Preserve Scores(Counter)
Open App.Path & "\ReturnChamp.txt" For Input As #1
Input #1, Names(Counter), Scores(Counter)
Close #1
Wend
An infinite loop for a multi-line file. :eek2:
J/K it'd error unless there's a file numbered 1 already open(in which case it'd again error if you tried to reuse that number). ;)
FTFY:
Code:
Dim Names() as String, Scores() as Integer
Dim Counter as Integer
Open App.Path & "\ReturnChamp.txt" For Input As #1
Do While Not EOF(1)
ReDim Preserve Names(Counter)
ReDim Preserve Scores(Counter)
Counter=Counter + 1
Input #1, Names(Counter), Scores(Counter)
Loop
Close #1
'NOTE: You should write the file with Output(or Append) mode and Write #.
'like this:
Open App.Path & "\ReturnChamp.txt" For Output As #1
For Counter = 0 to uBound(Names-1)
Write #1, Names(Counter), Scores(Counter)
Next Counter
Close #1
Re: Writing Information Onto Text File
Quote:
An infinite loop for a multi-line file.
The loop stops when it reaches the end of the file.
Re: Writing Information Onto Text File
FireXtol, your code works perfectly! I decided to go with it! :check:
Thanks, all, for your help! This thread is now resolved.
Re: [RESOLVED] Writing Information Onto Text File
I got a problem...
I thought it worked right, but I cannot write the information onto the text file at all. Did I have an error somewhere in the following code:
Code:
Option Explicit
Dim gstrChampName As String, PrevWinnings As Long
Private Sub Form_Load()
Dim saScore() As String, ChampTmpstr As String, tmpstr As String, tmplng As Long
gstrChampName = "Jonathan"
PrevWinnings = 23000
Open App.Path & "\ReturnChamp.txt" For Append As #1
Print #1, ChampTmpstr
If InStr(ChampTmpstr, "|") Then
saScore = Split(ChampTmpstr, "|")
tmpstr = saScore(0): tmplng = CLng(saScore(1))
gstrChampName = tmpstr: PrevWinnings = tmplng
End If
Close #1
End Sub
?
Re: [RESOLVED] Writing Information Onto Text File
Are you using Vista or W7 and is your application in Program Files or some other protected folder? Because an application needs admin rights to be able to write to locations like Program Files, Windows, System folder etc.
Re: [RESOLVED] Writing Information Onto Text File
I'm using Windows XP.
But what do you mean by administration rights?
Re: [RESOLVED] Writing Information Onto Text File
Quote:
Originally Posted by
JonSea31
But what do you mean by administration rights?
Read post #4 in this thread for some details regarding UAC http://www.vbforums.com/showthread.php?t=617268
EDIT: Your code doesn't work because you write the ChampTmpstr variable to the file
Print #1, ChampTmpstr
but it is declared just above that, and you don't set its value anywhere so it's empty. What your code does basically is open the file, write nothing to it and close it.
Re: [RESOLVED] Writing Information Onto Text File
So would I be required to replace the ChampTmpstr in the Print #1 line with something else? What do you recommend?
Re: [RESOLVED] Writing Information Onto Text File
You need to replace it with the variable that's holding the data you want to write to the file.
Re: Writing Information Onto Text File
Post deleted by the User.