c_cpp read_write_data_as_raw.cpp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp read_write_data_as_raw.cpp相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <fstream>
using namespace std;

struct Data {
  int number;
  double buy;
  double sale;
  int amount;
};

int main(void) {
  int number = 1;
  int padding = -1;
  double buy = 3.4;
  double sale = 5.2;
  int amount = 30;

  ofstream out("data.out");
  out.write(reinterpret_cast<char*>(&number), sizeof(int));
  out.write(reinterpret_cast<char*>(&padding), sizeof(int)); // There is padding here
  out.write(reinterpret_cast<char*>(&buy), sizeof(double));
  out.write(reinterpret_cast<char*>(&sale), sizeof(double));
  out.write(reinterpret_cast<char*>(&amount), sizeof(int));
  out.close();

  ifstream in("data.out");
  Data receiver;

  while (true) {
    in.read(reinterpret_cast<char*>(&receiver), sizeof(Data));
    cout << "{"
         << receiver.number << ", "
         << receiver.buy << ", "
         << receiver.sale << ", "
         << receiver.amount
         << "}" << endl;
    if (in.eof()) break;
  }

}

以上是关于c_cpp read_write_data_as_raw.cpp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp -_-公元前

c_cpp uv__next_timeout.c

c_cpp uv__handle_unref.c

c_cpp uv__handle_ref.c

c_cpp uv__handle_stop.c

c_cpp uv__handle_start.c