Results 1 to 1 of 1

Thread: Classic VB - How to do basic manipulation of text files

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow Classic VB - How to do basic manipulation of text files

    Classic VB has a built in set of keywords held over from the legacy BASIC era, used for dealing with IO. As you should be aware there are two directions of data flow, input and output. Let's look at both of these in application to text files.

    The first thing we must do before working with a file is to open it. The Open statement takes the filename and the open mode, in which we specify how we're going to use the file. If we try and use it in a way we didn't specify, VB will slap us on the wrist.

    To open a file you need a file handle which is a number uniquely identifying the opened file within the application workspace. To get the next available file handle we call the FreeFile() function.
    VB Code:
    1. Dim fileID As Long
    2. fileID = FreeFile()

    Let's create a file called "C:\example.txt". If this file already exists, it will be overwritten since we are going to specify that we want to write to it.
    VB Code:
    1. Open "C:\example.txt" For Output As #fileID

    If you want, you can specify that you want to lock the file from being written to by other applications.
    VB Code:
    1. Open "C:\example.txt" For Output Lock Write As #fileID

    Now the file is opened, we can perform operations on it. In Output mode we can do Print's and Write's. Write is used when treating a file like a mini database. It surrounds the text outputted with quote marks.

    Let's print a simple line to the file.
    VB Code:
    1. Print #fileID, "I come from a land down under"

    Simple

    Once we're done with the file we must always close it.
    VB Code:
    1. Close #fileID
    This frees up the file handle and makes sure changes to the file are commited and it can be used by other applications.


    Now let's look at reading from the file. There are two main ways we can do this, Input mode and Binary mode.

    Input mode is best for reading one line at a time. To do this we use the Line Input statement, which reads the next line of the file into a buffer variable. The EOF() function returns True if we have hit the end of the file, so we loop while it is false to read all of the file.
    VB Code:
    1. Open "C:\example.txt" For Input Lock Write As #fileID
    2. Dim line As String
    3. Do While (Not EOF(fileID))
    4.   Line Input #fileID, line
    5.   MsgBox(line)
    6. Loop
    7. Close #fileID

    Binary is good for reading the whole thing in one go, or chunks of the file when you know the start byte and byte length that you want to read. In Binary mode we can use the Input and Get statements. Both require a buffer variable, like Line Input. The Input statement sets the size of the buffer variable for you, whereas Get determines how much data to read by how long the buffer variable already is.

    Here is an example of reading an entire file into a string. Here I am using the Get statement, which takes file handle, start byte (optional), and buffer variable. The amount of data read is determined by the length of the buffer. This method is very efficient.
    VB Code:
    1. Open "C:\example.txt" For Binary Access Read Lock Write As #fileID
    2. Dim data As String
    3.  
    4. ' Set the size of the data buffer to the size of the file
    5. data = Space(LOF(fileID))
    6.  
    7. ' Read the data
    8. Get #fileID, , data
    9.  
    10. Close #fileID

    Binary mode also allows you to write data. To do this we can use the Put statement. Put is simply the reverse of Get, it writes the data to the file at the specified starting position.
    VB Code:
    1. Open "C:\example.txt" For Binary Access Write Lock Read Write As #fileID
    2. Dim data As String
    3.  
    4. data = "Test string"
    5.  
    6. ' Read the data
    7. Put #fileID, , data
    8.  
    9. Close #fileID

    As ever if you have any further Q's please post in Classic VB. If you have any further explanations or examples then you can post in this thread.

    Cheers
    - P
    Last edited by penagate; May 14th, 2006 at 06:51 AM. Reason: additions + corrections

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