如何从.dat文件中提取资源文件?

问题描述 投票:-2回答:1

我已经有了关于.dat文件的提取器源代码。但是这个来源用于其他游戏。

然而,这两场比赛是在同一家公司制作并保持类似的格式。

所以我想得到帮助。

// Sample Code.....
#define WIN32_LEARN_AND_MEAN

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <memory.h>
#include <direct.h>


struct dat
{ // total 25byte
    unsigned long offset; // 4byte
    char name[13]; // 13byte
    unsigned long size; // 4byte;
    char *data; // 4byte;
};


int main(int argc, char **argv)
{
    int value;
    char input[512];
    printf(" * Select mode\n  1: Pack\n  2: Unpack\n Choose: ");
    scanf("%d", &value);
    if(value == 1)
    {
        printf("\n\n *Mode: Pack\n  Datname: ");
        scanf("%s", input);
        dat_pack(input);
    }
    else if(value == 2)
    {
        printf("\n\n *Mode: Unpack\n  Datname: ");
        scanf("%s", input);
        dat_unpack(input);
    }
    printf("\nPress any key to continue\n");
    fflush(stdin);
    getch();
    return 0;
}


int dat_unpack(char *file_name)
{
    FILE *fp;
    fp = fopen(file_name, "rb");

    if (fp == 0)
    {
        printf(" File does not exist: %s\n", file_name);
        return 0;
    }
    else
    {
        long i;
        long len;
        long total;
        struct dat *dat;
        fread(&total, 4, 1, fp);
        len = sizeof(struct dat);
        dat = malloc(len*total);
        memset(dat, 0, len*total);
        *strstr(file_name, ".") = 0; // cute trick :p

        printf("\n reading Infomation... ");

        for (i = 0; i<total; i++)
        {
            fread(&(dat[i]), 17, 1, fp);
            if (i > 0) dat[i - 1].size = dat[i].offset - dat[i - 1].offset;
        }

        printf("ok.\n Total %d data(s) in dat file.\n", --total);

        for (i = 0; i<total; i++)
        {
            file_write(&(dat[i]), fp, file_name);
        }

        printf(" Unpack Complete!\n");

        free(dat);
        fclose(fp);
    }

    return 1;
}

这个来源可以打开下一个东西。

那么,mon.dat is working upper source.

现在我想从“.dat文件”中提取一些看起来像这样的东西:

cthugha.dat - it was not working from upper extract sources :(

请让我知道我需要做什么才能提取下面列出的cthugha.dat文件的组件。

c extract
1个回答
1
投票

由于您标记为C ++,您可以使用类对记录建模,然后编写一些方法从二进制流中读取类(对象):

class Record
{
  public:
    unsigned long size;
    unsigned long offset; 
    std::string   name;
    std::vector<uint8_t> data;

  void load_from_file(std::istream& input);
};

void Record::load_from_file(std::istream& input)
{
  // Use uint8_t as a byte.
  offset = 0UL;
  for (size_t i = 0; i < 4; ++i)
  {
    uint8_t byte;
    input.read((unsigned char *) &byte, 1);
    // Assume Big Endian
    offset = offset * 256 + byte;
  }
  // read in other fields from file.
  //...
}

将类建模到数据文件记录允许程序利用更安全的数据结构。

输入方法允许从数据文件格式转换为平台格式。例如,输入文件允许您将多字节整数读入平台的格式,而不管平台的字节顺序如何。

std::vector允许在不知道运行时数据量的情况下包含数据。

用法示例:

std::vector<Record> database;
Record r;
while (true)
{
  r.load_from_file(data_file);
  if (data_file)
  {
    database.push_back(r);
  }
  else
  {
    break;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.