Reading a Text File in VB6

  • David MacDonald
    Participant

    Does anybody have a working example of reading in a text file that has multiple date in one line? I have it working for reading in an entire line of text and storing that in an array, but wanted to know if anyone can show me how to read in data separated by a comma on the same line. e.g Name, Age, Form Class all in the same line but read in and stored in 3 separate arrays?

    Thanks!

    chalove
    Keymaster

    David,

    It would be okay to read from the file and then use code to substring the file using the deliminators.

    Some languages will read text from files a line at a time, others allow you to set up records. As long as learners have a method for filling the array(s) I think it will be acceptable.

    Charlie

    Colin McAlpine
    Participant

    dim allPeople(1 to 100) as yourRecordTypeName

    counter =0
    dim filenumber as integer

    fileNUmber = freefile

    open filename blah blah blah for input
    Do while not eof(filenumber)
    counter = counter + 1

    input #filenumber, allPeople(counter).forename, allPeople(counter).surname,allPeople(counter). height, allPeople(counter).weight
    ‘or for single arrays
    input #filenumber, forename, surname, height, weight

    count = count + 1

    loop

    close file

    ‘would read in a four column CSV file for the number of items it has

    sgould
    Participant

    I managed to create a solution in VB Express 2010 using a Microsoft.VisualBasic.FileIO.TextFieldParser object. It allows you to specify a delimiter, read each line from the file into an array then access each field using the ReadFields() command.

    This web page was useful for explaining how it works and giving an example to work from – http://msdn.microsoft.com/en-us/library/cakac7e6.aspx

    Steven.

    Callum Dunlop
    Participant

    VB6 uses Write # and Input # as simple IO so the following code fragment both reads and writes different types of data:

    Dim FHandle As Integer
    Dim TestString As String, TestInt As Integer, TestReal As Double, TestDate As Date

    TestDate = Now

    FHandle = FreeFile
    Open “C:Test.txt” For Output As FHandle
    Write #FHandle, “String”, 12, 13.4, TestDate
    Close FHandle

    Open “C:Test.txt” For Input As FHandle
    Input #FHandle, TestString, TestInt, TestReal, TestDate
    Close FHandle

    Note that when you use Write #, certain field types are delimited for simple input parsing so your text file would look like:

    “String”,12,13.4,#2014-10-23 16:01:41#

    For multiple elements or records, repeatedly use write # and input # (before closing the file :-)).

    Cheers, C.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.