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

时间:2012-01-10 03:14:37

标签: 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;
}

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

2 个答案:

答案 0 :(得分:1)

代码运行正常。但是,您永远不会检查文件是否已成功打开以进行写入,因此它可能会在您的系统上无声地失败。打开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;
}

或者那些东西。另外,我不明白你为什么要先检查文件是否存在,因为无论文件是否存在都是如此。

答案 1 :(得分:0)

这应该有效

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