Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with code
#1
Rainbow 
Hey,

I was making custom pak file reader (some kind of container), and i have some wrong stuff.

When i write my data into this pak file - it writes fine, but when i'm trying to read it, it reads wrong.
Header stuff reads right, something wrong with file reading

See screenshot, it returns wrong filelen and pos also not full filename
[Image: EZX1p.png]

there's code, any idea?

Code:
typedef struct
{
    char id[4];
    long dirofs;
    long dirlen;
} dpackheader_t;

typedef struct
{
    char name[128];

    long filepos;
    long filelen;
} packfile_t;

void FS_InitFiles ( void )
{
    datafile_t pak = datafile_t();
    pak.filename = "test.pak";
    pak.paknum = 1;

    ifstream inputFile( pak.filename,  ios::in | ios::binary );
    inputFile.seekg( 0, ios::end );
    long fileSize = inputFile.tellg();
    inputFile.seekg( 0, ios::beg );
    
    byte *pBuffer = new byte[fileSize];
    inputFile.read( (char*) pBuffer, fileSize );
    //inputFile.close();

    //pBuffer[fileSize]='\0';

    const byte *pPtr = pBuffer;
    dpackheader_t *pHeader = ( dpackheader_t* )pPtr;
    pBuffer[sizeof(pHeader->id)]='\0';
    pPtr += sizeof( dpackheader_t );

    pak.header = dpackheader_t();
   // pak.header.id = pHeader->id;//br.ReadChars(4);
    strcpy(pak.header.id, pHeader->id);
    pak.header.dirofs = pHeader->dirofs; //br.ReadInt32();
    pak.header.dirlen = pHeader->dirlen; //br.ReadInt32();

    if ( strncmp( pHeader->id, "PACK", 4 ) != 0 )
    {
        Com_Printf("It isn't .PAK");
        return;
    }

    Com_Printf("id: %s dirofs: %i dirlen: %i\n", pak.header.id, pak.header.dirofs, pak.header.dirlen);

    
    // ================== //
    pak.files = new packfile_t[pak.header.dirlen / 64];

    inputFile.seekg(pak.header.dirofs, ios::beg);
    pBuffer = new byte[pak.header.dirofs];
    inputFile.read( (char*) pBuffer, fileSize );
    
    byte *pPtrs = pBuffer;
    //pBuffer[sizeof(pHeaders->name)]='\0';
    pPtrs += sizeof( packfile_t );

    // temp log
    fstream myfile;
    myfile.open ("t.txt");

    //pPtrs -= pHeaders->filelen;
    // NOTE: packfile_t is s*, use in sizeof

    int k;
    for(k = 0; k < pak.header.dirlen / 64; k ++)
    {
        packfile_t *s = (packfile_t*)pPtrs;
        pPtrs += (pak.header.dirlen/64); //sizeof( packfile_t );

        pPtrs -= (sizeof(s->filelen) - strlen ( s->name ) - sizeof(s->filepos));
        pPtrs += sizeof(s);

        pBuffer[sizeof(s->name)]='\0';    
        
        Com_Printf("File #%i - filepos: %i filelen - %i filename: %s\n", k, s->filepos, s->filelen, s->name);

        pPtrs += sizeof( s );

        myfile << str("%s\n", s->name);

    }

    delete[] pBuffer;
    myfile.close();
    inputFile.close();
}

Thanks
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click

#2
Oh, i am sorry, i solved this. Can be closed.
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click

#3
Maybe you can add your solution to the topic before we close it.

#4
Just some lines, lol
fread(&header, sizeof(header), pak);
and
fread (pakfile, 1, header.dirlen, pak);
Then use pakfile[i]->file info.
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click

#5
Thanks, topic closed because of the request from the topicstarter.



Possibly Related Threads…
Thread Author Replies Views Last Post
  Help Code color crosshairs koren30 3 4,015 10-02-2013, 19:26
Last Post: koren30
  Help need help?how to make plugins code hXnarutoXone 12 8,544 09-01-2013, 18:30
Last Post: Bandarigoda123
  Help Need Help with C# code tubwux 2 3,311 08-27-2013, 18:18
Last Post: tubwux
  [Request] Compile this code please dozsa0 4 4,131 08-10-2013, 21:02
Last Post: Nukem
  Compile this code please First_Semyon 12 9,681 08-08-2013, 14:53
Last Post: Bandarigoda123
  Compile please this code First_Semyon 8 5,637 07-28-2013, 01:52
Last Post: First_Semyon
  Code of vector Bloodfocus 1 2,410 06-23-2013, 11:54
Last Post: Yamato
  problem with gsc code CheGuevara 5 5,616 04-20-2013, 15:06
Last Post: Nekochan
Tongue [Request] read if you have a cs:go code:) rawr-saours 5 4,061 04-05-2013, 18:15
Last Post: SuperNovaAO
Smile Help Help me with a code (who knows) NyZzE 2 2,540 04-05-2013, 01:47
Last Post: NyZzE

Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.