简体   繁体   English

C ++读取二进制数据以构造

[英]C++ reading binary data to struct

I am currently reading a binary file that i know the structure of and i am trying to place into a struct but when i come to read off the binary file i am finding that when it prints out the struc individually it seems to come out right but then on the fourth read it seems to add it onto last member from the last read. 我目前正在读取一个我知道其结构的二进制文件,并尝试将其放入一个结构中,但是当我读取该二进制文件时,我发现当它单独打印出结构时,看起来似乎正确,但是然后在第四次读取时,似乎会将其添加到最后一次读取的最后一个成员上。

here the code which probably make's more sense than how i am explaining it: 这里的代码可能比我解释它更有意义:

Struc 结构

#pragma pack(push, r1, 1)
struct header
{
    char headers[13];
    unsigned int number;
    char date[19];
    char fws[16];
    char collectversion[12];
    unsigned int seiral;
    char gain[12];
    char padding[16];

};

Main 主要

  header head;
  int index = 0;
  fstream data;
  data.open(argv[1], ios::in | ios::binary);
  if(data.fail())
    {
      cout << "Unable to open the data file!!!" << endl;
      cout << "It looks Like Someone Has Deleted the file!"<<endl<<endl<<endl;
      return 0;

    }
   //check the size of head
   cout << "Size:" << endl;
   cout << sizeof(head) << endl;
   data.seekg(0,std::ios::beg);




   data.read( (char*)(&head.headers), sizeof(head.headers));

   data.read( (char*)(&head.number), sizeof(head.number));

   data.read( (char*)(&head.date), sizeof(head.date));

   data.read( (char*)head.fws, sizeof(head.fws));


//Here im just testing to see if the correct data went in.
   cout<<head.headers<< endl;
   cout<<head.number<< endl;
   cout<<head.date<< endl;
   cout<<head.fws<< endl;


   data.close();

   return 0;

Output 输出量

Size:
96
CF001 D 01.00
0
15/11/2013 12:16:56CF10001001002000
CF10001001002000

for some reason the fws seems to add to head.date? 由于某些原因,fws似乎添加到head.date? but when i take out the line to read head.fws i get a date that doesn't have anything added? 但是当我取出一行来阅读head.fws时,我得到的日期没有添加任何内容?

i also know thier more data to get for the header but i wanted to check the data up to what i have written is correct 我也知道需要更多的数据来获取标题,但我想检查数据,直到我写的正确为止

cheers 干杯

1. Your date is declared as: 1.您的日期声明为:

char date[19];

2. Your date format is exactly 19-characters long: 2.您的日期格式正好为19个字符长:

15/11/2013 12:16:56

3. And you print it this way: 3.然后以这种方式打印:

cout<<head.date

Shortly speaking, you try to print fixed char[] using its address, which means, that it will be interpreted as null-terminated c-string. 简而言之,您尝试使用其地址打印固定的char[] ,这意味着它将被解释为以null终止的 c字符串。 Is it null-terminated? 它是空终止的吗? No. 没有。

To solve this problem, declare date as: 要解决此问题,请将date声明为:

char date[20];

And after you fill it, append null terminator: 并填充后,附加空终止符:

date[19] = 0;

It applies to all members, that will be interpreted as string literals. 它适用于所有成员,将被解释为字符串文字。

You have char date[19] filled with 15/11/2013 12:16:56 which is exactly 19 valid characters. 您的char date[19]充满15/11/2013 12:16:56 ,正好是19个有效字符。 This leaves no space for a terminating null and so doing cout << head.date outputs your 19 valid characters and then a load of garbage. 这样就没有空格来终止空值,因此执行cout << head.date输出您的19个有效字符,然后输出大量垃圾。

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

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