Results 1 to 16 of 16

Thread: [RESOLVED] Writing Information Onto Text File

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Resolved [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?

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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
    Last edited by baja_yu; Jun 16th, 2010 at 07:17 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    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?

  4. #4
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Writing Information Onto Text File

    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.
    Last edited by rajbdilip; Jun 17th, 2010 at 06:26 AM.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  5. #5
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    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:
    1. Dim saScore() as String
    2. Open (App.Path & "\ReturnChamp.txt") For Input As 1
    3.   Do While Not EOF(1)
    4.     Line Input #1, ChampTmpstr
    5.     If instr(ChampTmpstr,"|") then
    6.         saScore = Split(ChampTmpstr, "|")
    7.         tmpStr = saScore(0): tmpLng = CLng(saScore(1))
    8.         RedChampName = tmpstr: PrevWinnings = tmplng
    9.     Else
    10.         Msgbox "Error Parsing Scores!"
    11.     End If
    12.   Loop
    13. Close 1

    You both missed the file number of the Close statement, though. Unless the intention is to close all open files.

  6. #6
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    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
    Last edited by rajbdilip; Jun 17th, 2010 at 06:36 AM.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  7. #7
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Writing Information Onto Text File

    Quote Originally Posted by rajbdilip View Post
    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.

    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

  8. #8
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Writing Information Onto Text File

    An infinite loop for a multi-line file.
    The loop stops when it reaches the end of the file.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: Writing Information Onto Text File

    FireXtol, your code works perfectly! I decided to go with it!

    Thanks, all, for your help! This thread is now resolved.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    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
    ?

  11. #11
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: [RESOLVED] Writing Information Onto Text File

    I'm using Windows XP.

    But what do you mean by administration rights?

  13. #13
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] Writing Information Onto Text File

    Quote Originally Posted by JonSea31 View Post
    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.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    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?

  15. #15
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: Writing Information Onto Text File

    Post deleted by the User.
    Last edited by JonSea31; Aug 5th, 2010 at 10:05 AM. Reason: Decided to Create Separate Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width