Easy Method of Writing And Reading Small Text Files

In a production application, every time you want to access a file for reading or writing, you have to retrieve a free handle using the FreeFile() function to make sure that you do not overwrite an existing handle. Not only this, but you have to remember to close the file after you are done with it.

In some specific cases, you can avoid the trouble by wrapping this functionality inside utility functions.

For example, I wrote the following two functions to go between strings and text files in my apps.


public Function ReadFile(FileName as string) as string

    Dim i as Integer
    i = FreeFile

    on error GoTo ErrorTrap

    Open FileName for input as #i
    ReadFile = input(LOF(i), i)
    Close #i

    Exit Function

ErrorTrap:

    ReadFile = ""

End Function


public Sub WriteFile(FileName as string, Contents as string)

    Dim i as Integer
    i = FreeFile

    Open FileName for Output as #i

    print #i, Contents
    Close #i

End Sub

Once these functions are in your project, you have a quick way of reading and writing text files. For example, the following code is a weird way of copying text files:


Call WriteFile("c:\b.txt", ReadFile("c:\a.txt"))



Comments

  • overwriting

    Posted by steved3298 on 09/17/2004 09:35pm

    Is there any way to stop the program from writing to the first line and instead writing to the next line w/o text?

    Reply
  • how to implement this code

    Posted by Legacy on 06/14/2003 12:00am

    Originally posted by: Danny Pryor

    this question has already been answered , but here is the answer once again ;
    
    

    Call WriteFile("c:\myfile.txt")

    Call ReadFile("c:\myfile.txt")

    Reply
  • Your code returned false ; this code returns true

    Posted by Legacy on 04/02/2003 12:00am

    Originally posted by: Danny

    Function OpenText(FileDir As String)
    
    Dim i As Integer
    i = FreeFile
    FileExist = Dir(FileDir)
    If FileExist = "" Then
    MsgBox "File Not Found!", vbCritical + vbSystemModal, "Error"
    Exit Function
    End If
    Open (FileDir) For Input As #i
    OpenText = Input(LOF(i), i)
    Close #i
    End Function

    Reply
  • File write

    Posted by Legacy on 03/02/2003 12:00am

    Originally posted by: rahaman

    I am having a doubt in file sysyem object.can you please clarify it please. I have to create a text file in which the datas should be written in specified location and printed using vb code.Please help me.
    

    Reply
  • Reading and Writing text Files in VB

    Posted by Legacy on 01/28/2002 12:00am

    Originally posted by: Ignacio Diaz

    Tried it and like it very good code. Made my life simpler.
    Thanks...

    Reply
  • Flaw

    Posted by Legacy on 03/21/2000 12:00am

    Originally posted by: Aaron Wacker

    If you read using input, then write using print, you are adding 2 characters to the file so it would be different than the original file. Try running the procedures recursively and you will see what I mean..

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds