Results 1 to 5 of 5

Thread: Text file --->>> string

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    36

    Post

    Dead simple I'm sure - but I have no help file (no msdn CD)

    How do I read the contents of a text file into a string?

    THANX
    Konan of the five hundred and fifty fith veriety

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Here's a Function I wrote to make it really simple, (this will read any file upto 2GB in size):
    Code:
    Function ReadFile(ByVal FileName As String) As String
        'Open Specified File and Return Data in a String
        Dim iFile As Integer
        Dim lFileLen As Long
        Dim lChunk As Long
        Dim sChunk As String
        Dim sData As String
        
        On Error GoTo ReadFileErr
        iFile = FreeFile
        Open FileName For Binary Access Read As iFile
        lFileLen = LOF(iFile)
        lChunk = 64000  'Load File in 64K Chunks
        While Loc(iFile) < lFileLen
            'Create a Buffer to Retrieve File Data
            If Loc(iFile) + lChunk > lFileLen Then lChunk = lFileLen - Loc(iFile)
            sChunk = Space(lChunk)
            'Retrieve the next Chunk of Data from the File
            Get #iFile, , sChunk
            'Append the Chunk to the Data Retrieved so far
            sData = sData & sChunk
        Wend
        Close iFile
        'Return the File Data
        ReadFile = sData
    ReadFileErr:
    End Function
    Usage: sFileString = ReadFile("C:\TheFile.txt")

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    36

    Post TA m8

    I'll give it a whirl - tanx for yer code, I'll include yre name if I distribute my app - is that OK?

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Sure, no problem, anything I post here is Public Domain, (anyone can use/copy/mangle any/all of it), sometimes I'll add a comment requesting a mention of Credit if they do though.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    36

    Post cool

    I'm just haveing a look at how it works - when I tried useing a binary file in qbasic I just got number junk when I looked at it (numbers - no text) - but If I put it back it became the original again. However I put this to use in a cool little encryption app

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