从二进制文件c读取16位整数++ [英] Reading 16-bit integers from binary file c++

查看:416
本文介绍了从二进制文件c读取16位整数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如果我这样做的权利,所以我想检查我的code。它的工作原理,但我不知道它的工作的权利。我需要它来读取二进制文件,而16位整数存储在int数组所需要的确切大小。我试着做的sizeof(存储[I])所以我可以看到,如果我是存储16位,但它说,32(我猜是因为INT自动分配的4个字节?

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. 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?

        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;
      }
 }

::::::::::::::::::::::::::::::::::::::::::::;

::::::::::::::::::::::::::::::::::::::::::::;

我改读语句:

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

和数组键入。现在它的pretty除了输出口工作是有些奇怪:

and the array to type 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

我不知道什么是在.bin文件,但我猜0不应该存在。笑

I'm not sure what's in the .bin file, but I'm guessing the 0 shouldn't be there. lol

推荐答案

是,int是4个字节(在32位x86平台)。

Yes, int is 4 bytes (on 32-bit x86 platform).

您有两个问题:


  1. 亚历克蒂尔评论中提到正确的,你有你的存储声明为int,这意味着4个字节。这不是个问题,真的 - 你的数据将适合

  2. 实际的问题:你的线,正在读文件: inputFile.read((字符*)及存储[测试]的sizeof(2)); 实际上是读4个字节,因为2是整数,所以的sizeof(2)为4。你不需要的sizeof

  1. As Alec Teal correctly mentioned in comment, you have your storage declared as int, which means 4 bytes. 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)); is actually reading 4 bytes, because 2 is integer, so sizeof(2) is 4. You don't need sizeof there.

这篇关于从二进制文件c读取16位整数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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