Results 1 to 6 of 6

Thread: Reading file from PChar...

  1. #1

    Reading file from PChar...

    Hi all,

    again another question,
    Is it possable to read file information from a file stored in a PChar?

    e.g.
    [pascal]
    type
    TFile = Record
    Data: PChar;
    // other information
    end;

    TPak = class
    files: Array of Tfile;
    // other information
    end;

    procedure TPak.LoadPakFile
    begin
    // read pak file
    For i := 0 To files count -1 Do
    Begin
    // read file information
    stream.read(files[i].data^, files[i].size);
    End;
    // other information
    close stream
    end;
    [/pascal]

    I want to load the pak file at the beginning of the program and keep it in memory, but i have no idea how i can read the file data from the pak file item!
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Reading file from PChar...

    A PChar can act as a buffer which you could write to a memeory stream. Then you could read the file from the memory stream into whatever structure you want.

    Another way is to
    [pascal]
    Type
    TCharArray : Array[1..65000] of Char;

    ...

    Data := TCharArray(PChar);
    [/pascal]
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Re: Reading file from PChar...

    Quote Originally Posted by M109uk
    Hi all,

    again another question,
    Is it possable to read file information from a file stored in a PChar?

    I want to load the pak file at the beginning of the program and keep it in memory, but i have no idea how i can read the file data from the pak file item!
    I think you'll better drop the pchar and use TMemoryStream instead, that is more Pascal and OOP
    Memory streams are streams stored in ram, wh ere you can define the size. A buffer, in other word
    You can create a memory stream and load the file there. Then you can pass the stream to all functions like "loadfromstream". Also, you can use the read methods to read the data, and you can still access to the pointer with the appropriate field (if i remember well it's MemoryStream.memory )

    Bye!
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  4. #4

    Reading file from PChar...

    Personally I prefer the PChar method over the TMemoryStream method, the main reason is that the TMemoryStream comes with a lot of overhead and in the end the TMemoryStream is using a Pointer in almost the same way you would be using the PChar anyways. The advantage of the TMemoryStream is that it comes with build in features to read things from a pointer other than (and larger than) characters, but mimicing that behaviour with a PChar or a Pointer is not hard either!

    However MSX is right, using TMemoryStream is more the Delphi way, I'm just old-fashioned and like to do everything on my own! I'm one of those Delphi developers that only use objects where needed, and functions and where he can!

    So in the end... I guess it's a style thing!
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  5. #5

    Reading file from PChar...

    Thanx all for your suggestions,
    i dont really want to use any more memory than needed because the main pak file will be very large, at the moment it is 40mb+ this will contain textures, models, actors, world data, and other data files.

    Is there any point of keeping the data stored in memory or would it be more memory friendly to just load the data straight from the pak stream?

    If i keep the PChar data how will i be able to load it in to a structure?

    thanx
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  6. #6

    Reading file from PChar...

    Another option is to use memory mapped files.
    Anyway you can always look how pak file support was realized in Delphi remake of Quake II: http://www.sulaco.co.za/quake2/ (even in pascal)
    There are only 10 types of people in this world; those who understand binary and those who don't.

Bookmarks

Posting Permissions

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