File Handling Read and Write Binary Data
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct MyStruct
{
int a;//2
float b;//4
double c;//8
}obj;
int main() {
obj.a = 10;
obj.b = 30;
obj.c = 4000;
ofstream file("abc.txt", ios::binary | ios::out);
if (file.is_open()) {
file.write(reinterpret_cast<char*>(&obj), sizeof(obj));
file.close();
cout << "File Create Sucess.";
}
//=============Read===========
ifstream file_read("abc.txt", ios::binary | ios::out);
if (file_read.is_open()) {
file_read.read(reinterpret_cast<char*>(&obj), sizeof(obj));
file_read.close();
cout << "File Read Sucess.";
}
cout << "A=" << obj.a << endl;
cout << "C=" << obj.b << endl;
cout << "B=" << obj.c << endl;
return 0;
}
Comments
Post a Comment