简体   繁体   English

从文件读取数据到 C 中的结构

[英]Reading data from a file to a struct in C

Lets say i used the fread function to read data from a file to a struct.假设我使用 fread function 将数据从文件读取到结构。 How exactly is data read to the struct?数据究竟是如何读取到结构中的? Lets say my struct has the following:可以说我的结构具有以下内容:

Int x;
Char c;

Will the first 4 bytes read go into x and the next byte go into c?前 4 个字节会将 go 读入 x 并将下一个字节 go 读入 c 吗? And if i read in more bytes than the elements in my struct can hold what's gonna happen?如果我读取的字节数超过了我的结构中的元素可以容纳的内容会发生什么?

Will the first 4 bytes read go into x and the next byte go into c?前 4 个字节会将 go 读入 x 并将下一个字节 go 读入 c 吗?

Yes, unless your compiler has extremely strange padding rules (eg every member must be 8 byte aligned).是的,除非您的编译器有非常奇怪的填充规则(例如,每个成员都必须是 8 字节对齐的)。 And assuming Int is 4 bytes and Char is 1 byte.并假设Int是 4 个字节, Char是 1 个字节。

And if i read in more bytes than the elements in my struct can hold what's gonna happen?如果我读取的字节数超过了我的结构中的元素可以容纳的内容会发生什么?

That's undefined behavior, unless perhaps the over-long write is not more than sizeof(YourStruct) in which case you'll only be writing to the padding bytes (which on a lot of platforms will be 3 bytes after the char).这是未定义的行为,除非超长写入可能不超过sizeof(YourStruct)在这种情况下,您只会写入填充字节(在许多平台上将是 char 之后的 3 个字节)。

fread reads data byte-for-byte from a file (stream) into memory. fread从文件(流)中逐字节读取数据到 memory。 Therefore, if what you're trying to read is a struct, the byte layout of the struct in the file must exactly match the layout your compiler has chosen for the struct in memory.因此,如果您要读取的是结构,则文件中结构的字节布局必须与编译器为 memory 中的结构选择的布局完全匹配。

So the question of "How does fread read from a file?"所以“ fread如何从文件中读取”的问题? really boils down to, "How does the compiler lay out structs in memory?"真的归结为,“编译器如何在 memory 中布置结构?”

And the answer to that question is, it's partly determined by the rules of the C language, and it's partly up to the compiler.而这个问题的答案是,它部分取决于 C 语言的规则,部分取决于编译器。

So if you want to read structures from a file, you have three choices:所以如果你想从文件中读取结构,你有三个选择:

  1. Learn everything you can about the C rules for laying out structures in memory, and the choices compilers can make in interpreting these rules.了解有关在 memory 中布局结构的 C 规则的所有信息,以及编译器在解释这些规则时可以做出的选择。 Keep all these rules in mind as you design your structures and your data file formats.在设计结构和数据文件格式时,请牢记所有这些规则。 (This is not an impossible task. Many programmers take this approach to file i/o all the time.) (这不是一项不可能完成的任务。许多程序员一直采用这种方法来归档 i/o。)

  2. Don't worry about the layout too much.不要太担心布局。 Define your structures, and write them out to files using fwrite .定义您的结构,并使用fwrite将它们写入文件。 Then the files are automatically readable using fread -- at least, as long as the program doing the reading is running on the same kind of machine, and was compiled by the same compiler using the same settings.然后文件可以使用fread自动读取——至少,只要执行读取的程序在相同类型的机器上运行,并且由相同的编译器使用相同的设置编译。 (This, too, is a popular strategy, and works much of the time.) (这也是一种流行的策略,并且在很多时候都有效。)

  3. Don't use fread to read structures form a file.不要使用fread从文件中读取结构。 (And although it sounds defeatist, this is my own preferred argument.) (虽然这听起来很失败,但这是我自己喜欢的论点。)

There's much, much more that could be said abut this question.关于这个问题,还有很多很多可以说的。 If you choose approach 1, as I've already said, you're going to have to learn everything you can about the C rules for laying out structures in memory, and the choices compilers can make in interpreting these rules.如果您选择方法 1,正如我已经说过的,您将不得不学习有关在 memory 中布局结构的 C 规则的所有内容,以及编译器在解释这些规则时可以做出的选择。 If you choose approach 3, you have to learn some decent techniques for doing so without using fwrite and fread .如果您选择方法 3,则必须学习一些不错的技术来做到这一点,而无需使用fwritefread But I'm not going to launch into long explanations of either of those topics here.但我不打算在这里对这些主题中的任何一个进行冗长的解释。 I'm sure someone else will post some links, or you could start with Chapter 17 of these C programming notes .我相信其他人会发布一些链接,或者您可以从这些C 编程说明第 17 章开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM