Results 1 to 14 of 14

Thread: Getting data from a text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    16
    How would I do this, we are working on a program where questions are taken out of a text file and used in this trivia game we are making. Another problem we have to solve is how to store the number of question you are, or the point of the game you are in. Would you just use a static variable? Thanks for all the help
    Drew

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    This will load all qwestions into an array of variable length strings:
    Code:
    'in declarations
    Private Qwestions() as string
    'loading procedure
    Dim buffer as string
    Open file for binary as 1
      if lof(1) then
        buffer=space(lof(1))
        Get#1,,buffer
        qwestions=split(buffer,vbcrlf)
      end if
    close 1
    And you should use a private variable for score or public/global if you have several forms.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    16

    thanks kedeman

    Now, how should I prepare the text file? Thanks
    Drew

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Posts
    81

    Talking Another way to do it

    kedaman, I think the newer way of opening text files is easier to understand. You can tell the die-hard VB programmers by how they open files

    Another way of doing it is to make a reference to Microsoft Scripting Runtime control using Project>>References. Then the following code (which I find easier to understand) should work.

    Code:
    'Declarations
    Dim FSys as New FileSystemObject
    Dim fileData As String
    Dim InStream As TextStream
    Dim lines() As String
    
    'remainder of code
    Private Sub Form_Load()
    
    'set InStream to hold the text coming from textfile.txt
    Set InStream = FSys.OpenTextFile("C:\windows\textfile.txt")
    
    'store all the data from textfile.txt to the variable fileData
    fileData = InStream.ReadAll
    
    'split the data from the textfile into lines (a line break is indicated by vbCrLf
    lines = Split(fileData, vbCrLf)
    
    'now each line that was previously in the textfile has been
    'stored in the array [lines(0), lines(1), lines(2), ... etc.]
    
    End Sub
    Both ways should work equally well, but the way I've described is the newer way. I think it's easier - personal preference I suppose

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    kedaman:
    Qwestions Payback: LOL

    wandoprog

    How do I do this?
    What is happening?
    Why?

    the split breaks the file an each VBCrlf.



    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    oh ouch! Sam, why do i have to learn FSO when i can do everything i need with VB? Might want to place a file control on the form instead which you can use Or why not implementing a loadtext method in every textbox label and anything so that you don't need to handle files in vb?

    I know some ppl like a object oriented interface, but beginners might not. wandoprog, what do you mean by prepare the textfile?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    16
    when i say "prepare the text file" i mean how do you type in the information so that vb can open it, and use it.

    would you do it like this...

    "question1" , "Answer1"
    "question2" , "Answer2"

    i know that doesn't work, but how do you do it? thanks
    Drew

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    as for FileSystemObject, you are adding controls and
    dll to your project. Vb adds enough on it's own without
    add further undue garbage.

    Code:
    If using the split method you could use 2 files
    
    QuestionFile:
    How do I do this? 
    What is happening? 
    Why? 
    
    AnswerFile:
    I don't know.
    Not too much.
    Anyone's guess as to why.
    
    use the code above on both. 
    You now have 2 arrays of equal measure.
    
      ArrayOne              ArrayTwo
    myQuestionOne(1)  =     myAnswer(1)
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Lively Member
    Join Date
    Mar 2000
    Posts
    81
    Kedaman: every man to his own, I guess. I like FSO, and I'm not too keen on that Open file For Whatever As #1. Personal preference. I get your point though, and to be fair I think it'd be worth learning both methods and using them accordingly, depending on which one suited me best at the time.

  10. #10
    Guest
    Another method of opening.
    Code:
    Open "MyFile" For Input As #1
    MyStr = Input(LOF(1), 1)
    Close #1

  11. #11
    Guest
    Here is how you can retrieve the data in the text file in the form of Question,Answer and stroe them in an Array respectivly.

    Add the following to a Form with 2 CommandButtons.
    Code:
    Private Question(5) As String
    Private Answer(5) As String
    
    Private Sub Command1_Click()
    
        Dim tmp As String
        Dim Var As Variant
        Dim iCount As Integer
        
        Open "C:\Windows\Desktop\MyFile.txt" For Input As #1
        Do Until EOF(1)
            DoEvents
            Line Input #1, tmp
            iCount = iCount + 1
            Var = Split(tmp, ",")
            Question(iCount) = Var(0)
            Answer(iCount) = Var(1)
        Loop
        
    End Sub
    
    Private Sub Command2_Click()
        'Display the Questions ans Answers
        For I = 0 To 5
            Print Question(I)
            Print Answer(I)
            Print ""
        Next I
    End Sub

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Don't use Input function to read the whole file, it's much faster to read it in binary, and it's also faster to split than read the file in input.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    16

    is there a snippet of code to...

    convert something to binary? Thanks
    drew

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You don't need to worry about how your data is stored, just in what order it is, when you open binary files. Just use the code i gave you, it will split all the qwestions separated by carriage return line feeds, into Qwestions array.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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