Last active
November 21, 2016 13:56
-
-
Save LusainKim/891488e790cbc9ad0fb9e5de2fc9c196 to your computer and use it in GitHub Desktop.
fstream example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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