Skip to content

Instantly share code, notes, and snippets.

@LusainKim
Last active November 21, 2016 13:56
Show Gist options
  • Save LusainKim/891488e790cbc9ad0fb9e5de2fc9c196 to your computer and use it in GitHub Desktop.
Save LusainKim/891488e790cbc9ad0fb9e5de2fc9c196 to your computer and use it in GitHub Desktop.
fstream example
#include <iostream>
#include <fstream>
using namespace std;
struct Data
{
char name[20];
int dummy;
};
void Save(Data &sample)
{
ofstream out;
out.open("file.txt", ios::out);
if (!out.is_open()) exit(0);
out.write(reinterpret_cast<const char*>(&sample), sizeof(Data));
out.close();
}
void Load(Data &sample)
{
ifstream in;
in.open("file.txt", ios::in);
if (!in.is_open()) exit(0);
in.read(reinterpret_cast<char*>(&sample), sizeof(Data));
in.close();
}
int main()
{
Data Savesample { "Lusain", 1011101 };
Data Loadsample;
Save(Savesample);
Load(Loadsample);
cout << Loadsample.name << endl;
cout << Loadsample.dummy << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment