Results 1 to 6 of 6

Thread: reading one character from a file at a time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    MFAU
    Posts
    19

    Post

    i wish to open a file in binary mode (so i can read each character at a time, including EOF, CR, LF etc) using a Do Until EOF loop.
    i think i am supposed to use GET but i am not sure how to read a single character, i can read the whole file at once, but this is not what i want to do. thanks in advance

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111

    Post

    Reading the whole file then using a string function would be faster i.e.
    Code:
    'LoadFile here
    TheFileContents = 'What's in the file
    For x = 1 to len(TheFileContent)
    TheChr = mid$(TheFileContent,x,1)
    'Things you want to do with this character
    Next x
    Hope I helped,


    ------------------
    DiGiTaIErRoR
    VB, QBasic, Iptscrae, HTML
    Quote: There are no stupid questions, just stupid people.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    MFAU
    Posts
    19

    Post

    sorry, i didnt explain why i cant do that, i have to do it one byte at a time.. because i will be reading large files (ie > 50 MB) and these will not all fit into one string, they may fit in a variant but i am not willing to use all the ram that way

  4. #4
    Guest

    Post

    You could declare a user defined type of one string character
    Code:
    Type OneChar
       OneByte   As    String  * 1
    End Type
    Then you could open the file for random and access it one character at a time:
    Code:
    Dim iChar As OneChar
    Dim iFileNum As Integer
    Dim iRecLength As Long
    Dim iLastRec As Long
    
    iRecLength = Len(iChar)
    iFileNum = FreeFile
    Open "yourfile.txt" For Random As iFileNum Len = iRecLength
    iLastRec = FileLen("yourfile.txt") / iRecLength
    Then you loop through the records(actually characters) using the Get Statement.

    Code:
    For x = 1 To iLastRec
      Get #iFleNum, x, iChar
    Next x
    Hope that works..

    ------------------
    Boothman
    There is a war out there and it is about who controls the information, it's all about the information.
    [This message has been edited by Boothman_7 (edited 01-25-2000).]

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    MFAU
    Posts
    19

    Post

    will that method work with binary access? because if i open it with random i will not be able to read EOF, of which there will be several scattered through the file.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    MFAU
    Posts
    19

    Post

    tried to do a
    Code:
    Dim MyVar as OneByte
    didnt work ;P

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