处理二进制文件 - 模板化函数问题

发布于 2024-09-11 04:37:53 字数 938 浏览 7 评论 0原文

我创建了一个用于处理二进制文件的小工具。所有涉及读取文件和处理文件的函数都是模板化的,类似于:

template <class T> void processFile( const char* fileName );

模板参数T用于确定将被读取并被视为一项的数据的大小。我不知道如何准确地说,所以一个小例子(请注意,这些是用十六进制编辑器看到的二进制数据)。文件内容:

BEEF1234DEAD5678

T 为无符号字符,项目为:BE EF 12 34 DE AD 56 78
T 为无符号整数时,项目为:BEAF1234 DEAD5678
T 为双精度:BEAF1234DEAD5678

(请注意,我在这里假设 unsigned char 为 1 个字节,unsigned int是 4 个字节,double 是 8 个字节大。)T 也用于 STL 容器,因为我经常使用它们(向量、映射和列表是用于处理文件所涉及的许多函数)。对于内置数据类型(char、int 等),一切都运行得很好。

现在我的问题是什么:我希望能够使用 1、4、8 以外的尺寸。例如16、32甚至6、10、14等。据我所知,没有这些尺寸的内置类型。我怎样才能实现这个目标?我主要关心的是我需要 STL 容器中的数据并使用 sort() 等。 POD 结构有效吗?请注意,我主要使用 C 函数进行读取(这里没有流,但有一些 memcpy 和 memcmp、fread 等)。

很抱歉没有提供更多代码,我现在正在使用我的备用笔记本电脑。但我相信不需要更多的代码。如果有的话,我明天就提供。

I've created a small tool which is used to process binary files. All the functions involved in reading the files and processing them are templated and are similar to this:

template <class T> void processFile( const char* fileName );

The template parameter T is used to determine the size of data which will be read and treated as one item. I dont know how to say it precisely, so a tiny example (note that these are binary data as seen with a hexeditor). File contents:

BEEF1234DEAD5678

With T being unsigned char, items are: BE EF 12 34 DE AD 56 78
With T being unsigned int, items are: BEAF1234 DEAD5678
With T being a double: BEAF1234DEAD5678

(Note that I assume here that unsigned char is 1 byte, unsigned int is 4 bytes and double is 8 bytes large.) The T is also used for STL containers, because I use them a lot (a vector, a map and a list are used in many of the functions involved in processing files). Everything works perfectly fine with built-in datatypes (char, int etc.).

Now what is my problem: I would like to be able to work with sizes different from 1,4,8. For example 16, 32 or even 6, 10, 14 etc. As far as I know, there are no built-in types of these sizes. How can I achieve this? My main concern is that I need the data in STL containers and use sort() for example. Will a POD structure work? Note that I'm using mainly C functions for reading (no streams here, but some memcpy and memcmp, fread etc.).

Sorry for not providing more code, I'm using my spare laptop now. But i believe more code should not be necessary. If so, I will provide it tomorrow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

护你周全 2024-09-18 04:37:53

如果我正确理解你的问题,答案是肯定的:你应该能够使用合适的 POD 类型来专门化你的模板函数。但是,您需要定义一个成员 operator<() 才能使用 std::sort()

在一般情况下,以下 POD 可能对您有用(它肯定比双打排序更好):

template <int N>
struct MyFill{
  char contents[N];
  bool operator<(const MyFill<N>& other){
    for (int i=0; i<N; ++i){
      if (contents[i]!=other.contents[i]){
        return (contents[i]<other.contents[i]);
      }
    }
    return false;
  }
};

If I understand your question correctly, the answer is yes: you should be able to specialize your template function with a suitable POD type. However you'll need to define a member operator<() in order to be able to use std::sort().

The following POD might be useful to you in the general case (it will certainly sort better than doubles):

template <int N>
struct MyFill{
  char contents[N];
  bool operator<(const MyFill<N>& other){
    for (int i=0; i<N; ++i){
      if (contents[i]!=other.contents[i]){
        return (contents[i]<other.contents[i]);
      }
    }
    return false;
  }
};
肤浅与狂妄 2024-09-18 04:37:53

使用运算符<<提取数据。这样T的大小以及能否排序就不是你的问题了。因此,对于 C++ 流,我们放弃 fscanf 转而使用 std::ifstream 是有原因的。

事实上,对于很多很多 C++ 类型来说,memcpy 或 memcmp 并不安全,使用它们是你绝对应该戒掉的习惯。

Use operator<< to extract data. This way the size of T and whether or not it can be sorted is not your problem. So get C++ streams, there's a reason we ditched fscanf for std::ifstream.

It's actually not safe to memcpy or memcmp many, many C++ types, and using them is a habit that you definitely should cut.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文