【问题标题】:How to fread() structs?如何 fread() 结构?
【发布时间】:2014-03-10 10:00:29
【问题描述】:
struct book
{
    unsigned short  size_of_content;
    unsigned short  price;
    unsigned char  *content;
};

假设我有一个包含多个books 的文件,每个都有不同的size_of_contentpricecontent。我如何一次阅读一本book 并确定它是哪本书(例如查看价格)?

size_t nread2;
struct book *buff = malloc(sizeof(struct book));
while( (nread2 = fread(buff, sizeof(struct book), 1, infp)) > 0 )
{
    printf("read a struct once \n");
}

这是我目前所拥有的。每当我阅读结构时,我都会尝试打印。但是,当我尝试使用 5 个结构的输入文件时,它会打印 15 次...

谢谢。

【问题讨论】:

  • 您尝试读取的文件格式是什么?
  • @Maxpm 可以是任何东西,但我拥有的测试文件是一个 bin 文件。
  • 读懂写文件的函数,你就知道怎么读了。
  • 考虑为此使用外部库,例如 sqlite。
  • 这对我来说似乎是一个家庭作业问题。如果是这样,SQLite 不太可能有帮助......文件格式将是家庭作业的一部分。如果这是一个实际问题,那么请使用 SQLite 或其他一些库。

标签: c file file-io struct io


【解决方案1】:

让我们看看你的struct,想想它有多大。

struct book {
    unsigned short  size_of_content;
    unsigned short  price;
    unsigned char  *content;
};

第一项是unsigned short,没问题,sizeof(unsigned short)大概会是2,如2字节。下一个也一样。

但是第三个。那是指向unsigned char 的指针...您的磁盘记录不太可能是保存的指针。你有一个字段size_of_content...我猜磁盘记录包含size_of_content,然后是price,然后是实际内容。

我不会为你编写完整的代码,但在伪代码中它是这样的:

fread(&size_of_content, sizeof(size_of_content), 1, infp)
sanity-check the value of size_of_content and handle any error
fread(&price, sizeof(price), 1, infp)
sanity-check teh value of price and handle any error
buff->content = malloc(size_of_content)
check for error on malloc and handle any error
fread(buff->content, size_of_content, 1, infp)

如果您对内容的大小没有严格的规范,请假设它不能超过 10 亿或类似的东西,并确保该数字至少不这么大!始终检查错误。

由于struct 中只有两个字段,因此很容易每个字段都设置fread()。如果你有一个更复杂的结构,使用结构可能是值得的:

struct book_header {
    unsigned short  size_of_content;
    unsigned short  price;
};

struct book {
    struct book_header header;
    unsigned char *content;
}

然后您可以使用fread()sizeof(book_header) 一口气读取整个标题。在处理波形音频文件等二进制文件时,我编写了很多这样的代码。


您可能不必担心这一点,但如果文件是在“大端”计算机上写入并在“小端”计算机上读取的,反之亦然。

http://en.wikipedia.org/wiki/Endianness

如果您确实有这个问题,解决方案就是标准化。选择其中一个(little-endian 或 big-endian)并使用 C 库函数来确保使用该 endian-ness 写入和读取数字。比如写时htonl()库函数,读时ntohl()

http://linux.die.net/man/3/htonl

但正如我所说,您可能不需要担心这个。

【讨论】:

  • 这只是一个随意的问题,内容的实际大小不会很大?
  • 我无法猜测内容的实际大小可能有多大,但我不希望它很大。你可以看一下输入文件的大小,就知道肯定是5条记录,所以大概是文件大小的1/5左右吧。
  • 非常感谢!我仍然不确定的一件事是,由于文件中有多本书,fread 怎么知道要查找哪本书?
  • sizeof(unsigned short) 可能仍然是 2 字节,无论 32 位还是 64 位系统,因为大多数系统现在使用 LP64 或 LLP64 模型,其中 int 及以下不改变其大小
  • @LưuVĩnhPhúc,感谢您的指正。我累了,不知何故没有注意到这是short。更正了,现在我去睡觉了。
猜你喜欢
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
相关资源
最近更新 更多