简体   繁体   English

从二进制文件C ++读取16位整数

[英]Reading 16-bit integers from binary file c++

I'm not sure if I'm doing this right so I want to check my code. 我不确定我是否做对了,所以我想检查一下我的代码。 it works but I'm not sure its working right. 它有效,但我不确定它是否正确。 I need it to read the binary file, and store the 16 bit integers in an array of ints that is the exact size needed. 我需要它来读取二进制文件,并将16位整数存储在所需的确切大小的整数数组中。 I tried to do sizeof(storage[i]) so I could see if I was storing 16 bits but it says 32 (I'm guessing because int automatically allocates 4 bytes? 我试图做sizeof(storage[i])所以我可以查看是否存储了16位,但是它说32(我猜是因为int自动分配了4个字节?

        void q1run(question q){
        int end;
        std::string input = q.programInput;
        std::ifstream inputFile (input.c_str(), ios::in | ios::binary);                     //Open File
        if(inputFile.good()){                                       //Make sure file is open before trying to work with it
                                                                    //Begin Working with information
           cout << "In File:  \t" << input << endl;
           inputFile.seekg(0,ios::end);
           end=inputFile.tellg();
           int numberOfInts=end/2;
           int storage[numberOfInts];
           inputFile.clear();
           inputFile.seekg(0);
           int test = 0;


           while(inputFile.tellg()!=end){       
               inputFile.read((char*)&storage[test], sizeof(2));
               cout << "Currently at position" << inputFile.tellg() << endl;
               test++;
           }

           for(int i=0;i<numberOfInts;i++){
               cout << storage[i] << endl;
           }
       }else{
           cout << "Could not open file!!!" << endl;
      }
 }

EDIT:::::::::::::::::::::::::::::::::::::::::::::; 编辑:::::::::::::::::::::::::::::::::::::::::::::;

I changed the read statement to: 我将read语句更改为:

      inputFile.read((char*)&storage[test], sizeof(2));

and the array to type short . 和数组键入short now Its pretty well working except the output is a little strange: 现在它工作得很好,除了输出有点奇怪:

      In File:        data02b.bin
      8
      Currently at position4
      Currently at position8
      10000
      10002
      10003
      0

I'm not sure what's in the .bin file, but I'm guessing the 0 shouldn't be there. 我不确定.bin文件中有什么,但是我猜测0不应该在那里。 lol 大声笑

Use: int16_t in <cstdint> . 使用: int16_t<cstdint> (guaranteed 16bits) (保证16位)

Short s and int s can be of various sizes depending on the architecture. 根据体系结构, Shortint可以具有各种大小。

Yes, int is 4 bytes (on 32-bit x86 platform). 是的,int是4个字节(在32位x86平台上)。

You have two problems: 您有两个问题:

  1. As Alec Teal correctly mentioned in comment, you have your storage declared as int, which means 4 bytes. 正如Alec Teal在评论中正确提到的那样,您将存储声明为int,这意味着4个字节。 Not a problem, really - your data will fit. 没问题,真的-您的数据可以容纳了。
  2. Actual problem: your line which is reading the file: inputFile.read((char*)&storage[test], sizeof(2)); 实际问题:正在读取文件的行: inputFile.read((char*)&storage[test], sizeof(2)); is actually reading 4 bytes, because 2 is integer, so sizeof(2) is 4. You don't need sizeof there. 实际上读取4个字节,因为2是整数,所以sizeof(2)是4。您不需要那里的sizeof

Storage is declared as an 'array' of "int"s, sizeof(int)=4 存储被声明为“ int”的“数组”,sizeof(int)= 4

This shouldn't be a problem though, you can fit 16 bit values in 32 bit spaces, you probably meant short storage[... 不过,这应该不成问题,您可以在32位空间中容纳16位值,这可能意味着short storage[...空间short storage[...

Additionally for the sake of full-disclosure, sizes are defined in terms of sizeof(char) as a monotonically increasing sequence. 另外,为了完全公开,根据sizeof(char)将大小定义为单调递增的序列。

4 is by far the most common though, hence the assumption. 4是迄今为止最常见的一种假设。 (Limits.h will clarify) (Limits.h将阐明)

One way to store 16 bits integers is to use the type short or unsigned short . 存储16位整数的一种方法是使用shortunsigned short类型。

You used sizeof(2) which is equal to 4 because 2 is of type int so the the way to read 16 is to make storage of type short and the read: 您使用的sizeof(2)等于4,因为2是int类型的,因此读取16的方法是使类型为short storage和读取:

short storage[numberOfInts];
....    
inputFile.read((char*)&storage[test], sizeof(short));

You can find here a table containing the sizes of all the types. 您可以在此处找到包含所有类型大小的表格。

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

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