如何将vector <unsigned char =“”>写入file,后跟unsigned int

时间:2016-01-12 06:59:30

标签: c++ file visual-studio-2013

我必须对5个unsigned char个向量进行分组(data_length=5)。在组的末尾,我想添加一个ID组并将它们(组数据+其ID)写入文件。哪种组ID类型integer。我按照以下代码执行该任务。这是对的吗?  吼叫图显示了我正在拍摄的内容

enter image description here

#define random(x) (rand()%x)
std::ofstream filewrite("abc.raw", std::ios::out | std::ofstream::binary);
unsigned int iter = 0;
unsigned int data_length=5;
unsigned int ID_data=0;
//-------------Write data-------------//
while (iter<10){
    vector<unsigned char> vec_data;
    for (unsigned int i=0;i<data_length;i++){
        vec_data.push_back(random(256))
    }           
    std::copy(vec_data.begin(), vec_data.end(), std::ostreambuf_iterator<char>(filewrite));
    //Write ID_data after vec_data
    filewrite.write(reinterpret_cast<const char *>(&ID_data), sizeof(ID_data));
    ID_data++;
    iter++;
}
filewrite.close();

另外,我想在没有组ID的情况下将数据提取到vector。这是我从上述文件中提取数据的代码,但它不会删除ID组。你能帮我把它删除吗?

//-------------Read data-------------//
std::ifstream file("abc.raw", std::ios::binary);
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);

// get its size:
std::streampos fileSize;

file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);

// reserve capacity
std::vector<unsigned char> vec;
     vec.insert(vec.begin(),
std::istream_iterator<unsigned char>(file),
std::istream_iterator<unsigned char>());

2 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解了你的问题。因为我假设您在二进制写入方面遇到问题,所以我只为一个向量提供解决方案。您可以轻松修改它:

void Write( std::ostream& os, const std::vector< std::uint8_t >& v, const std::int32_t id )
{
  std::size_t len = v.size();
  os.write( (const char*)&len, sizeof len );
  for ( auto e : v )
    os.write( (const char*)&e, sizeof e );
  os.write( (const char*)&id, sizeof id );
}

void Read( std::istream& is, std::vector< std::uint8_t >& v )
{
  std::size_t len;
  is.read( (char*)&len, sizeof len );
  v.resize( len );
  for ( auto &e : v )
    is.read( (char*)&e, sizeof e );
  std::int32_t id;
  is.read( (char*)&id, sizeof id );
}

int main()
{
  // write
  {
    std::ofstream os( "abc.raw", std::ios::binary );
    if ( ! os )
      return -1;
    std::vector< std::uint8_t > v;
    v.push_back( 0x10 );
    v.push_back( 0x20 );
    v.push_back( 0x30 );
    Write( os, v, 0x123 );
  }

  // read
  {
    std::ifstream is( "abc.raw", std::ios::binary );
    if ( ! is )
      return -1;
    std::vector< std::uint8_t > v;
    Read( is, v );
  }

  //
  return 0;
}

答案 1 :(得分:2)

首先,您使用while,其中for更容易阅读。 BTW。你不需要两个迭代变量。可以迭代ID_data

for( unsigned int ID_data = 0; ID_data < 10; ++ID_data ) {
    // ...
}

第二,你不需要生成一个你永远不会重用的向量,然后编写元素。很好直接写出值。

for( unsigned int ID_data = 0; ID_data < 10; ++ID_data ) {
    for( unsigned int i = 0; i < data_length; i++ )
        filewrite.put(random(256));

    filewrite.write(reinterpret_cast<const char *>(&ID_data), sizeof(ID_data));
}

第二部分:您可以使用两个迭代器实例化一个向量。

auto vec = std::vector<unsigned char>(std::istream_iterator<unsigned char>{file},
                                      std::istream_iterator<unsigned char>{});

但是,由于您只想阅读data_length值,您可能更喜欢:

auto vec = std::vector<unsigned char>{};

while( file ) {
    for( unsigned int i = 0; i < data_length; i++ ) {
        char c;
        if( !file.get(c) )
            break;
        vec.push_back(static_cast<unsigned char>(c));
    }

    unsigned int ID_data;
    file.read(reinterpret_cast<const char *>(&ID_data), sizeof(ID_data));
    // we don't use ID_data here, so it will simply be ignored.
}