繁体   English   中英

C ++中的二进制输入/输出

[英]Binary input/output in C++

我正在尝试一个相当简单的程序来测试二进制输入/输出。 我基本上是写一个带有标题(字符串)和一些数据(双精度)的文件。 代码如下:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>


int main() {
   typedef std::ostream_iterator<double> oi_t;
   typedef std::istream_iterator<double> ii_t;
   std::ofstream ofs("data.bin", std::ios::in);
   //-If file doesn't exist, create a new one now
   if(!ofs) {
      ofs.open("data.bin", std::ios::out|std::ios::binary|std::ios::app);
   }
   else {
      ofs.close();
      ofs.open("data.bin", std::ios::out|std::ios::binary|std::ios::app);
   }

   //-Write a header consisting of length of grid subdomain and its name
   ///*
   const std::string grid = "Header";
   unsigned int olen = grid.size();
   ofs.write(reinterpret_cast<const char*>(&olen), sizeof(olen));
   ofs.write(grid.c_str(), olen);
   //*/

   //-Now write the data
   ///*
   std::vector<double> data_out;
   //std::vector<std::pair<int, int> > cell_ids;
   for(int i=0; i<100; ++i) {
      data_out.push_back(5.0*double(i) + 100.0);
   }
   ofs << std::setprecision(4);
   std::copy(data_out.begin(), data_out.end(), oi_t(ofs, " "));
   //*/
   ofs.close();

   //-Now read the binary file; first header then data
   std::ifstream ifs("data.bin", std::ios::binary);
   ///*
   unsigned int ilen;
   ifs.read(reinterpret_cast<char*>(&ilen), sizeof(ilen));
   std::string header;
   if(ilen > 0) {
      char* buf = new char[ilen];
      ifs.read(buf,ilen);
      header.append(buf,ilen);
      delete[] buf;
   }
   std::cout << "Read header: " << header << "\n";
   //*/

   ///*
   std::vector<double> data_in;
   ii_t ii(ifs);
   std::copy(ii, ii_t(), std::back_inserter(data_in));
   std::cout << "Read data size: " << data_in.size() << "\n";
   //*/
   ifs.close();

   //-Check the result
   ///*
   for(int i=0; i < data_out.size(); ++i) {
      std::cout << "Testing input/output element #" << i << " : "
                << data_out[i] << "  " << data_in[i] << "\n";
   }
   std::cout << "Element sizes: " << data_out.size() << "  " << data_in.size() <<  
            "\n";
   //*/
   return 0;
}

问题是,当我尝试写入和读取(然后打印)标头和数据时,它会失败(我确认它当时不会读取数据,但正确显示了标头)。 但是,当我注释掉其中一个写部分(标头和/或数据)时,它正确显示了该部分,表明读操作有效。 我确定我没有正确阅读。 也许我在某处缺少了seekg的用法。

该代码对我来说运行良好。 但是,您永远不会检查文件是否已成功打开以进行写入,因此它可能在您的系统上无提示地失败。 当您打开ofs您应该添加

if (!ofs) {
    std::cout << "Could not open file for writing" << std::endl;
    return 1;
}

在打开ifs之后也是一样

if (!ifs) {
    std::cout << "Could not open file for reading" << std::endl;
    return 1;
}

或类似的规定。 我也不明白为什么要先检查文件是否存在,因为无论是否存在,都进行相同的操作。

这应该工作

#include <iostream>
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdint>

int main() {

    ifstream fin;
    fin.open("input.dat", std::ios::binary | std::ios::in);
    if (!fin) {
        cerr << "Cannot open file " << "input.dat" << endl;
        exit(1);
    }

    uint8_t input_byte;
    while (fin >> input_byte) {
        cout << "got byte " << input_byte << endl;
    }

    return 0;
}

暂无
暂无

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

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