Results 1 to 14 of 14

Thread: Question About Text Files

  1. #1

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

    Question About Text Files

    If I had two text files: one that contains a question, and the other that contains a series of answers, in the same fashion as Family Feud, and if the questions in the questions.txt file go like this:

    Code:
    1,Tell me a man's name that starts with the letter K.
    2,Name a food that's red on the inside.
    3,Name a famous Bob.
    And the answers in the answers.txt file go like this, for example:

    Code:
    1,Kenneth/Ken,36
    1,Keith,21
    1,Kevin,13
    1,Kyle,7
    2,Watermelon,42
    2,Strawberry,27
    2,Tomato,17
    2,Raw Beef/Steak,3
    3,Bob Hope,28
    3,Bob Barker,24
    3,Bob Dylan,19
    3,Bob Newhart,12
    3,Bob Saget,7
    3,Bob Marley,4
    3,Bob Denver,2
    3,Bob Eubanks,2
    If the first number on each line of the text files represents the question ID number in the questions.txt file, how do I retrieve the answers for that corresponding question number in the answers.txt file in order from top to bottom, and display them in a text label in VB?

    Also, if I were to count each instance of answers for that corresponding question number, how do I do it?

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,443

    Re: Question About Text Files

    Have you thought about using the INI-System?
    The Question-No. would be the Section, the Answers the keys, and the Value would be the value to the Key(s)
    Code:
    'INI-File "answers.ini"
    [1]
    Kenneth_Ken = 36
    Keith = 21
    Kevin = 23
    Kyle = 7
    [2]
    Watermelon = 42
    Strawberry = 27
    Tomato = 17
    Steak = 3
    [3]
    Bob_Hope = 28
    Bob_Barker = 24
    ....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

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

    Re: Question About Text Files

    Actually, I have the database done already and I have thousands of questions and corresponding answers up to this point (and more coming). so creating the INI format would create more work than I need, since I would have to redo the database for thousands of questions (about 5,000, to be exact), and redoing the database in INI format would be quite time-consuming in fact.

    But thanks anyways, Zvoni. If anybody can please help me out with my original question, that would be a great help.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,443

    Re: Question About Text Files

    The only way i can think of is to run through your answers-file sequentially, do a split on each line with comma as separator, compare arrLine(0) if it is your question-No.
    If yes, read arrLine(1) and arrLine(2) (Answer and Value).

    Problem: For a question which is at the end of your file (Say you have 1000 Questions) and you want to retrieve the answers for Question 967....... i think you see your problem.

    You have to rework your answer-file into the INI-Format only once.

    EDIT: After thinking on it, a "real" database (even if it's just Access) might be a better solution, since you could then use simple SQL-Queries
    SELECT Answer, Value FROM AnswerTable WHERE QuestionID = 3
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Question About Text Files

    I must admit, I'm a great 'lover' of ini files, and for this particular application (which would be better suited to a 'proper' SQL Database) would give you direct access to required answers, given the question number.

    How about a little conversion utility to convert from your csv approach to an ini format ?
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim strData As String
    Dim strAnswer() As String
    Dim strInFile As String
    Dim strOutFile As String
    Dim intFileIn As Integer
    Dim intFileOut As Integer
    Dim intLast As Integer
    strInFile = "C:\MyDir\Answers.txt"
    strOutFile = "C:\MyDir\Answers.ini"
    intFileIn = FreeFile
    Open strInFile For Input As intFileIn
    intFileOut = FreeFile
    Open strOutFile For Output As intFileOut
    Do
        Line Input #intFileIn, strData
        strAnswer = Split(strData, ",")
        If CInt(strAnswer(0)) <> intLast Then
            Print #intFileOut, "[" & strAnswer(0) & "]"
            intLast = CInt(strAnswer(0))
        End If
        Print #intFileOut, strAnswer(1) & " = " & strAnswer(2)
    Loop Until EOF(intFileIn)
    Close intFileIn
    Close intFileOut
    MsgBox "Conversion Complete"
    End Sub
    Last edited by Doogle; Oct 25th, 2013 at 06:07 AM.

  6. #6

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

    Re: Question About Text Files

    Quote Originally Posted by Doogle View Post
    I must admit, I'm a great 'lover' of ini files, and for this particular application (which would be better suited to a 'proper' SQL Database) would give you direct access to required answers, given the question number.

    How about a little conversion utility to convert from your csv approach to an ini format ?
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim strData As String
    Dim strAnswer() As String
    Dim strInFile As String
    Dim strOutFile As String
    Dim intFileIn As Integer
    Dim intFileOut As Integer
    Dim intLast As Integer
    strInFile = "C:\MyDir\Answers.txt"
    strOutFile = "C:\MyDir\Answers.ini"
    intFileIn = FreeFile
    Open strInFile For Input As intFileIn
    intFileOut = FreeFile
    Open strOutFile For Output As intFileOut
    Do
        Line Input #intFileIn, strData
        strAnswer = Split(strData, ",")
        If CInt(strAnswer(0)) <> intLast Then
            Print #intFileOut, "[" & strAnswer(0) & "]"
            intLast = CInt(strAnswer(0))
        End If
        Print #intFileOut, strAnswer(1) & " = " & strAnswer(2)
    Loop Until EOF(intFileIn)
    Close intFileIn
    Close intFileOut
    MsgBox "Conversion Complete"
    End Sub
    I will definitely give that a try, Doogle. Thanks!

    Once I get the answer file converted to .ini format, how would the program be able to rank the answers from top to bottom (from #1 at the top to the lowest-ranked answer), and how would the question number be located based on what question ID number was randomly selected, since the question ID number is within square brackets, and how would the answers and their numeric values be placed in text labels?

    LIke, say, if the program were to select Question ID #2 in the Questions.txt file, and in the answers.ini file, there are 4 answers on the survey. If the program were to look for Question ID #2 in the answers.ini file, and then look for the answers below it right until the first square bracket below the lowest-ranking answer, and display each answer and corresponding value in their own respective text labels, how would I do it?

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Question About Text Files

    JonSea....How would you like to share that db?
    I built a Family Feud Game, but have nowhere near the same number of Qs & As....just asking...Sammi
    (Sorry for the interuption guys....)

    EDIT: OR the text files...I can load into my db (access).

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,443

    Re: Question About Text Files

    Read all Keys from your section (=Question-No., Say Question 3), put them in a two-dimensional array, send that array to a sorting-function to sort it by value (in this case the second dimension)
    Then you would be always on the right side and not depending that your entries in the INI-File are in the correct order (from Top to Bottom)

    There are some more API's to read information from INI-Files (Get All section Names, Get all Keys within one section etc.), and Doogle is the man to know for it ;-)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  9. #9

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

    Re: Question About Text Files

    Well, I could try creating the INI file and trying to figure out how to retrieve the lines of code and place them in the appropriate text labels, but I will need Doogle's help on this task.

    As for right now, I think the Access Database is probably a good idea at this point, just for the time being. But if somebody can help me out with the retrieving of data from the INI file, I would appreciate that greatly.

    And what does two-dimensional array mean? What format is used to denote the two-dimensional array? Something along the lines of strAns(#, #)?
    Last edited by JonSea31; Oct 25th, 2013 at 08:57 AM.

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Question About Text Files

    Here's something to get you going
    Code:
    Option Explicit
    
    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" _
                        (ByVal lpAppName As String, _
                         ByVal lpReturnedString As String, _
                         ByVal nSize As Long, _
                         ByVal lpFileName As String) As Long
                         
    
    Private Const ANSWER_FILE As String = "C:\MyDir\Answers.ini"
    
    Private Function GetSortedAnswers(strQuestion As String) As String()
    Dim strBuffer As String
    Dim strTemp As String
    Dim strTemp1 As String
    Dim strInfo() As String
    Dim strData() As String
    Dim strT() As String
    Dim lngLen As Long
    Dim lngALen As Long
    Dim intI As Integer
    Dim intJ As Integer
    Do
        lngLen = lngLen + 100
        strBuffer = Space(lngLen)
        lngALen = GetPrivateProfileSection(strQuestion, strBuffer, lngLen, ANSWER_FILE)
    Loop Until lngALen <> lngLen - 2
    strInfo = Split(strBuffer, Chr(0))
    ReDim strData(UBound(strInfo) - 2, 1)
    For intI = 0 To UBound(strInfo) - 2
        strT = Split(strInfo(intI), "=")
        strData(intI, 0) = Trim$(strT(0))
        strData(intI, 1) = Trim$(strT(1))
    Next intI
    For intI = UBound(strData, 1) To 0 Step -1
        For intJ = 0 To intI - 1
            If CInt(strData(intJ, 1)) < CInt(strData(intJ + 1, 1)) Then
                strTemp = strData(intJ, 0)
                strTemp1 = strData(intJ, 1)
                strData(intJ, 0) = strData(intJ + 1, 0)
                strData(intJ, 1) = strData(intJ + 1, 1)
                strData(intJ + 1, 0) = strTemp
                strData(intJ + 1, 1) = strTemp1
            End If
        Next intJ
    Next intI
    GetSortedAnswers = strData
    End Function
    
    Private Sub Command1_Click()
    Dim strA() As String
    Dim intI As Integer
    strA = GetSortedAnswers("3")
    For intI = 0 To UBound(strA, 1)
        Debug.Print strA(intI, 0), strA(intI, 1)
    Next intI
    End Sub
    After converting your example to a .ini format the results were
    Code:
    Bob Hope      28
    Bob Barker    24
    Bob Dylan     19
    Bob Newhart   12
    Bob Saget     7
    Bob Marley    4
    Bob Denver    2
    Bob Eubanks   2
    EDIT: If you want them in ascending order then change
    Code:
            If CInt(strData(intJ, 1)) < CInt(strData(intJ + 1, 1)) Then
    to
    Code:
            If CInt(strData(intJ, 1)) > CInt(strData(intJ + 1, 1)) Then
    (Can't believe only 19 remembered Bob Dylan )
    Last edited by Doogle; Oct 26th, 2013 at 01:55 AM.

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Question About Text Files

    Quote Originally Posted by JonSea31 View Post
    And what does two-dimensional array mean? What format is used to denote the two-dimensional array? Something along the lines of strAns(#, #)?
    Conceptually you can think of a 2D array as a Grid. One dimension representing the Rows and the other the Columns. Each element is identified by it's row and column number.

    You'd define a static 2D array like 'Dim strArray(5,1)' which would define a Grid of 6 X 2 elements. It's completely up to you as to whether you consider it having 6 rows and 2 columns or 6 columns and 2 rows - but once you've made the decision you have to stick with it for that particular array.

    Similarly, you can think of a 3D array as a cubic with each element having a row, column and 'depth' (eg. Dim strArray(x,y,z)) and a 4D array as a row of cubics each element having the row number containing the cubic, and a row, a column and a depth within that cubic (eg. Dim strArray(r,x,y,z)). A 5D array would be a grid of cubics(eg. Dim strArray(r,c,x,y,z)) and a 6D array a cubic of cubics (eg. Dim strArray(r,c,d,x,y,z)), after that it gets a bit difficult to visualise.
    Last edited by Doogle; Oct 27th, 2013 at 05:54 AM.

  12. #12

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

    Re: Question About Text Files

    I did convert the contents of the Answers.txt file to .ini format, plus I got the contents of each answer to a question added to a control array. Thanks!

    Now, Doogle, I have one more question before I mark this thread resolved: If I were to open two text files (Questions.txt and Answers.txt) and consolidate the question and survey answers into one area, so that it appears like this:

    Code:
    [1]
    Tell me a man's name that starts with the letter K. <== from Questions.txt
    Kenneth/Ken = 36 <== from Answers.txt
    Keith = 21 <== from Answers.txt
    Kevin = 13 <== from Answers.txt
    Kyle = 7 <== from Answers.txt
    [2]
    Name a food that's red on the inside.
    Watermelon = 42
    Strawberry = 27
    Tomato = 17
    Raw Beef/Steak = 3
    [3]
    Name a famous Bob.
    Bob Hope = 28
    Bob Barker = 24
    Bob Dylan = 19
    Bob Newhart = 12
    Bob Saget = 7
    Bob Marley = 4
    Bob Denver = 2
    Bob Eubanks = 2
    What would be a good way to code it?

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Question About Text Files

    I found it is so much simpler using a small database for something like this. And, instead of two tables, I use just one (the queries to retrieve a q and n answers takes NO time of course). Thanks to you my DB now has about 7000 records, each one having fields for the answers and values. So, When I query for an answer, brigns back just one record's worth of fields. I HATE parsing text files...you never know what kind of 'character' may be lurking in there. :-)
    I hope Doog can answer you, as I am sure you are well beyond wanting to use a database approach.

    But if you did, here is a very small sample of the one I use for my FF project: (Notice, I have 8 'rounds', 3 rounds (instead of 4 as in TV show(s)), and 5 questions (I call rounds in the DB) to use in the 'fast money' "round".

    Attachment 106517

  14. #14
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Question About Text Files

    Quote Originally Posted by JonSea31 View Post
    What would be a good way to code it?
    The simplest method might be to use
    Code:
    [1]
    Question = Tell me a man's name that starts with the letter K.
    Kenneth/Ken = 36
    Keith = 21
    Kevin = 13
    Kyle = 7
    [2]
    Question = Name a food that's red on the inside.
    Watermelon = 42
    Strawberry = 27
    Tomato = 17
    Raw Beef/Steak = 3
    [3]
    Question = Name a famous Bob.
    Bob Hope = 28
    Bob Barker = 24
    Bob Dylan = 19
    Bob Newhart = 12
    Bob Saget = 7
    Bob Marley = 4
    Bob Denver = 2
    Bob Eubanks = 2
    and
    Code:
    Option Explicit
    
    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" _
                        (ByVal lpAppName As String, _
                         ByVal lpReturnedString As String, _
                         ByVal nSize As Long, _
                         ByVal lpFileName As String) As Long
                         
    
    Private Const ANSWER_FILE As String = "C:\MyDir\Answers.ini"
    
    Private Function GetQuestionAndSortedAnswers(strQuestionNumber As String, strQ As String) As String()
    Dim strBuffer As String
    Dim strTemp As String
    Dim strTemp1 As String
    Dim strInfo() As String
    Dim strData() As String
    Dim strT() As String
    Dim lngLen As Long
    Dim lngALen As Long
    Dim intI As Integer
    Dim intJ As Integer
    Do
        lngLen = lngLen + 100
        strBuffer = Space(lngLen)
        lngALen = GetPrivateProfileSection(strQuestionNumber, strBuffer, lngLen, ANSWER_FILE)
    Loop Until lngALen <> lngLen - 2
    strInfo = Split(strBuffer, Chr(0))
    ReDim strData(UBound(strInfo) - 3, 1)
    Do
        strT = Split(strInfo(intI), "=")
        If UCase(Trim$(strT(0))) <> "QUESTION" Then
            strData(intJ, 0) = Trim$(strT(0))
            strData(intJ, 1) = Trim$(strT(1))
            intJ = intJ + 1
        Else
            strQ = Trim$(strT(1))
        End If
        intI = intI + 1
    Loop Until intJ > UBound(strData, 1)
    For intI = UBound(strData, 1) To 0 Step -1
        For intJ = 0 To intI - 1
            If CInt(strData(intJ, 1)) < CInt(strData(intJ + 1, 1)) Then
                strTemp = strData(intJ, 0)
                strTemp1 = strData(intJ, 1)
                strData(intJ, 0) = strData(intJ + 1, 0)
                strData(intJ, 1) = strData(intJ + 1, 1)
                strData(intJ + 1, 0) = strTemp
                strData(intJ + 1, 1) = strTemp1
            End If
        Next intJ
    Next intI
    GetQuestionAndSortedAnswers = strData
    End Function
    
    Private Sub Command1_Click()
    Dim strA() As String
    Dim strQ As String
    Dim intI As Integer
    strA = GetQuestionAndSortedAnswers("3", strQ)
    Debug.Print "Question: "; strQ
    For intI = 0 To UBound(strA, 1)
        Debug.Print strA(intI, 0), strA(intI, 1)
    Next intI
    End Sub
    Last edited by Doogle; Oct 29th, 2013 at 07:18 PM.

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