读取二进制文件到一个结构(C ++) [英] Reading Binary File into a Structure (C++)

查看:182
本文介绍了读取二进制文件到一个结构(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一点不能够正确读取二进制文件到我的结构的问题。该结构是这样的:

So I'm having a bit of an issue of not being able to properly read a binary file into my structure. The structure is this:

struct Student
{
    char name[25];
    int quiz1;
    int quiz2;
    int quiz3;
};

有37个字节(从字符阵列25字节,并且每整数4字节)。我的.dat文件是185字节。这5名学生有3整数等级。因此,每个学生占用37个字节(37 * 5 = 185)。

It is 37 bytes (25 bytes from char array, and 4 bytes per integer). My .dat file is 185 bytes. It's 5 students with 3 integer grades. So each student takes up 37 bytes (37*5=185).

它看起来像这样以纯文本格式:

It looks something like this in plain text format:

Bart Simpson          75   65   70
Ralph Wiggum          35   60   44
Lisa Simpson          100  98   91
Martin Prince         99   98   99
Milhouse Van Houten   80   87   79

我能单独使用这种code来读取每个记录:

I'm able to read each of the records individually by using this code:

Student stud;

fstream file;
file.open("quizzes.dat", ios::in | ios::out | ios::binary);

if (file.fail())
{
    cout << "ERROR: Cannot open the file..." << endl;
    exit(0);
}

file.read(stud.name, sizeof(stud.name));
file.read(reinterpret_cast<char *>(&stud.quiz1), sizeof(stud.quiz1));
file.read(reinterpret_cast<char *>(&stud.quiz2), sizeof(stud.quiz2));
file.read(reinterpret_cast<char *>(&stud.quiz3), sizeof(stud.quiz3));

while(!file.eof())
{
    cout << left 
         << setw(25) << stud.name
         << setw(5)  << stud.quiz1
         << setw(5)  << stud.quiz2
         << setw(5)  << stud.quiz3
         << endl;

    // Reading the next record
    file.read(stud.name, sizeof(stud.name));
    file.read(reinterpret_cast<char *>(&stud.quiz1), sizeof(stud.quiz1));
    file.read(reinterpret_cast<char *>(&stud.quiz2), sizeof(stud.quiz2));
    file.read(reinterpret_cast<char *>(&stud.quiz3), sizeof(stud.quiz3));
}

我也得到一个很好看的输出,但我希望能够在同一时间在同一时间在一个整体结构看,每个结构的不只是个人会员。这code是相信什么需要我来完成任务,但是......它不工作(后我会告诉输出):

And I get a nice looking output, but I want to be able to read in one whole structure at a time, not just individual members of each structure at a time. This code is what I believe needed to accomplish the task, but... it doesn't work (I'll show output after it):

*不包括类似的部分尽可能的文件和结构声明的开口,等

*not including the similar parts as far as opening of the file and structure declaration, etc.

file.read(reinterpret_cast<char *>(&stud), sizeof(stud));

while(!file.eof())
{
    cout << left 
         << setw(25) << stud.name
         << setw(5)  << stud.quiz1
         << setw(5)  << stud.quiz2
         << setw(5)  << stud.quiz3
         << endl;

    file.read(reinterpret_cast<char *>(&stud), sizeof(stud));
}

OUTPUT:

OUTPUT:

Bart Simpson             16640179201818317312
ph Wiggum                288358417665884161394631027
impson                   129184563217692391371917853806
ince                     175193530917020655191851872800

只有一部分它不弄乱是第一个名字,之后,它的下了山。我已经试过一切,我不知道什么是错的。我甚至通过本书我有搜索,我找不到任何东西。在那里的东西是什么样子我已经和他们的工作,但由于某种奇怪的原因,我的没有。我做了file.get(CH)(CH是一个字符)的字节25和它返回K,这是ASCII 75 ..这是第一次测试成绩,所以,一切都在它应该是。它只是没有在我的阅读结构正常。

The only part it doesn't mess up is the first name, after that it's down the hill.. I've tried everything and I've no idea what is wrong. I've even searched through the books I have and I couldn't find anything. Things in there look like what I have and they work, but for some odd reason mine doesn't. I did the file.get(ch) (ch being a char) at byte 25 and it returned K, which is ASCII for 75.. which is the 1st test score, so, everything's where it should be. It's just not reading in my structures properly.

任何帮助将大大AP preciated,我只是坚持了这一点。

Any help would be greatly appreciated, I'm just stuck with this one.

编辑:收到你们意想不到的真棒投入这么大的量之后,我决定接受你的建议,并坚持阅读中一个成员在​​同一时间。我做的事情用功能更清洁和更小的。 再次感谢你们提供这样的快速和启发性的意见。这是多少AP preciated。

After receiving such a large amount of unexpected and awesome input from you guys, I've decided to take your advice and stick with reading in one member at a time. I made things cleaner and smaller by using functions. Thank you once again for providing such quick and enlightening input. It's much appreciated.

如果您有兴趣在不建议大多数一种变通方法,滚动向底部,由user1654209第三的答案。这种解决方法完美的作品,但看过所有的意见,看看它为什么不是青睐。

IF you're interested in a workaround that's not recommended by most, scroll towards the bottom, to the 3rd answer by user1654209. That workaround works flawlessly, but read all the comments to see why it's not favored.

推荐答案

您结构几乎肯定被填充到preserve其内容的对齐方式。这意味着,它不会是37个字节,并且不匹配会导致读数不同步的。纵观每串失去3个字符的方式,似乎它已被填补到40个字节。

Your struct has almost certainly been padded to preserve the alignment of its content. This means that it will not be 37 bytes, and that mismatch causes the reading to go out of sync. Looking at the way each string is losing 3 characters, it seems that it has been padded to 40 bytes.

由于填充可能是字符串和整数之间,甚至不是第一个记录正确读取。

As the padding is likely to be between the string and the integers, not even the first record reads correctly.

在这种情况下,我建议不要尝试读取你的数据作为二进制数据,并坚持阅读各个字段。这是更强大,特别是如果你甚至想改变你的结构。

In this case I would recommend not attempting to read your data as a binary blob, and stick to reading individual fields. It's far more robust, especially if you even want to alter your structure.

这篇关于读取二进制文件到一个结构(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆