Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: File_Open (Random) Question

  1. #1
    Member marcuslee's Avatar
    Join Date
    Sep 2008
    Location
    Kansas, USA
    Age
    42
    Posts
    222
    Rep Power
    38

    File_Open (Random) Question

    n = FILE_OPEN(FileName, Mode, [RecordSize])

    Is it possible to divide the RecordSize in a Random File? I've done it in other basic languages. You can specify what is in the record and you can refer to just one part of it in reading or writing to the file.

    Here's how it looks in Liberty Basic:

    [code=thinbasic]
    open "File.dat" for random as #dat len = 10
    field #dat, 5 as FirstPart$
    field #dat, 5 as SecondPart$
    [/code]

    Then, later in the program, you can refer to just one part of the record:

    [code=thinbasic]
    FirstPart$ = "76103"
    put #dat, RecordNum
    [/code]

    In this case, nothing is written to SecondPart$ if I understand how it works.

    Can this be done in thinBasic? Use Random files almost as mini-databases?


    Mark


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

    Re: File_Open (Random) Question

    Powerbasic does seem to support this type file access but Eros doesnt seem to of implemented the file handling in quite the same way.

    but there is no reason you couldnt write your own handler, just use the seek keyword to find the next record.

    [code=thinbasic]
    Status = FILE_Seek(FileHandle, recordnumber*recordlength) ' Set the current read position to 1st byte of the record
    buffer = FILE_Get (FileHandle, recordlength) ' Read recordlength bytes and add to buffer
    [/code]
    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

  3. #3
    Member marcuslee's Avatar
    Join Date
    Sep 2008
    Location
    Kansas, USA
    Age
    42
    Posts
    222
    Rep Power
    38

    Re: File_Open (Random) Question

    Then, I suppose to finish the script, you could do something like this:

    [code=thinbasic]
    ' Set the current read position to 1st byte of the record
    Status = FILE_Seek(FileHandle, recordnumber*recordlength)
    ' Read recordlength bytes and add to buffer
    buffer = FILE_Get (FileHandle, recordlength)
    ' Extract the first part of the record
    firstpart = Mid$(buffer, 1, 5)
    'Extract the second part of the record
    secondpart = Mid$(buffer, 6, 5)
    [/code]

    I've been looking around the net and have found the code structure below.

    [code=thinbasic]
    TYPE Person
    LName AS STRING * 12
    FName AS STRING * 8
    Age AS INTEGER
    END TYPE

    DIM P AS Person

    FileHandle = FILE_OPEN( FileName, "Random", LEN( P ))
    [/code]

    I'm guessing that it won't work with thinBasic. If so, how do you determine RecordLength? By knowing the length of each field in your file? For example, if you know that one of your fields is a five digit number and that your second field is a string that you allot 10 characters in length, do you set the RecordLength at 15? If I'm not making much sense, I'm sorry. I'm having a hard time trying to explain this.

    Arg!!


    Mark

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

    Re: File_Open (Random) Question

    I removed this post as it didnt work.
    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

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: File_Open (Random) Question

    Mike was faster,

    here is mine version :
    [code=thinbasic]
    uses "File", "Console"

    type tRecord
    LName AS STRING * 12
    FName AS STRING * 8
    Age AS BYTE
    end type

    dim item as tRecord

    file_kill("test.txt")

    dim f as dword = file_open("test.txt", "binary")

    item.LName = "Vader"
    item.FName = "Darth"
    item.Age = 40
    printl "Vader in the file"
    file_put_tRecord(f, item)

    item.LName = "Yoda"
    item.FName = "Mister"
    item.Age = 45
    printl "Yoda in the file"
    file_put_tRecord(f, item)


    print "Seeking first: "
    file_seek_tRecord(f, 1)
    item = file_get_tRecord(f)
    printl item.LName, item.FName, item.Age

    print "Seeking second: "
    file_seek_tRecord(f, 2)
    item = file_get_tRecord(f)
    printl item.LName, item.FName, item.Age

    file_close(f)

    waitkey

    function file_seek_tRecord(f as long, nth as long)
    file_seek(f, (nth-1)*sizeof(tRecord)+1)
    end function

    function file_put_tRecord(f as long, item as tRecord)
    file_put(f, PEEK$(varptr(item), sizeof(item)))
    end function

    function file_get_tRecord(f as long) as string
    function = file_get(f, sizeof(item))
    end function
    [/code]


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: File_Open (Random) Question

    hi petr , hi all...

    @petr: please can you test your script if it's running? I cannot do that... have got error messages... or I have done something wrong... ???

    stop: script is ok... have copied something wrong, thank you I was too silly...

    best regards, Lionheart
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  7. #7
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: File_Open (Random) Question

    hi marcuslee, petr, all

    have tested the interesting script and changed it with two new heroes to catch the knowledge ...

    only the result as *.txt file isn't so clear as sentence for me... I am wrong with something in the code, isn't it??? perhaps somebody can check it... would be nice...

    best regards, Lionheart
    ("mittagspause")
    Attached Images Attached Images
    Attached Files Attached Files
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: File_Open (Random) Question

    Frank,

    the output file is binary - that is why strings look ok, but numbers are "odd".
    Member Age is of type BYTE = 8 bits = 1 ASCII character.
    Member Hero is of type LONG = 32 bits = 4 ASCII characters.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

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

    Re: File_Open (Random) Question

    I've added 2 new functions that will allow to work directly with random files:

    [code=thinbasic]
    FILE_PUTR(fChannel, RecNumber, UDTVAriable) '---Used to store structures
    FILE_GETR(fChannel, RecNumber, UDTVariable) '---Used to read back structures
    [/code]

    Features will be present in next release.

    Ciao
    Eros
    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

  10. #10

    Re: File_Open (Random) Question

    Thank YOU.
    I needed those.

    Ralph

Page 1 of 2 12 LastLast

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
  •