Results 1 to 2 of 2

Thread: Some Basic File functions

  1. #1
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    160

    Some Basic File functions

    Thought i would post a simple example of some file handling.

    @Eros. Perhaps you could take chunks of this for the FILE section of the help file.

    [code=thinbasic]
    ''''''''''''''''''''''''''
    ' File Handling
    '
    ' FILE_OPEN
    ' FILE_SEEK
    ' FILE_GET
    ' FILE_CLOSE
    ' FILE_LOAD
    ' FILE_SAVE
    '
    ' By Abraxas 29-01-2008
    '
    ''''''''''''''''''''''''''
    USES "FILE"

    DIM FileHandle as DWORD
    DIM Status AS DWORD
    DIM TestString AS STRING
    DIM FileName as STRING
    DIM sMsg as STRING

    TestString = "1234567890ABCDEF" ' String to write to file
    FileName = APP_SCRIPTPATH + "test.txt" ' Build filename
    '
    ' Here are two methods of saving the test string to a file
    '
    ' First method
    '
    ' Limitations - it will only write what is in the SringBuffer
    ' Advantages - Handles opening/closing the file
    '
    Status = FILE_SAVE(FileName, TestString) ' Save string to File
    '
    ' Second method
    '
    ' Limitations - None (that i can think of)
    ' Advantages - You can add and remove data
    '
    ' The seek function can be used to move around the file to set the read / write position
    '
    FileHandle = FILE_OPEN (FileName, "BINARY") ' Open file for writing
    Status = FILE_PUT (FileHandle, TestString) ' Write String to File
    Status = FILE_Seek (FileHandle, 5) ' Set the current write position to 5th byte in the file
    Status = FILE_PUT (FileHandle, TestString) ' Write String to File
    Status = FILE_CLOSE(FileHandle) ' Release File
    '
    ' Now lets try and read some bytes back into memory
    ' Lets build the string "166"
    '
    sMsg = "Bytes read from file are " + $DQ
    FileHandle = FILE_OPEN(FileName, "BINARY") ' Open file for reading
    Status = FILE_Seek(FileHandle, 1) ' Set the current read position to 1st byte in the file
    sMsg += FILE_GET (FileHandle, 1) ' Read 1 byte and add to output message
    Status = FILE_Seek(FileHandle, 10) ' Reset the current File read position to 10th byte in the file
    sMsg += FILE_GET (FileHandle, 1) ' Read 1 byte and add to output message
    Status = FILE_Seek(FileHandle, 10) ' File read position has moved to the next byte so we need to Reset to 10th byte in the file
    sMsg += FILE_GET (FileHandle, 1) ' Read 1 byte and add to output message
    Status = FILE_CLOSE(FileHandle) ' Release File

    sMsg += $DQ + $CRLF + $CRLF
    sMsg += "File Contents " + $DQ
    sMsg += FILE_LOAD(FileName) ' Read File into Ouput string buffer
    sMsg += $DQ

    Msgbox 0, sMsg ' Output string buffer

    [/code]

    thx

    Abx
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,795
    Rep Power
    10

    Re: Some Basic File functions

    Thanks a lot Abraxas

    I will for sure add the code in help docs.
    Great.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •