Skip to content

Instantly share code, notes, and snippets.

@dbasilioesp
Last active May 30, 2016 12:11
Show Gist options
  • Save dbasilioesp/1d723dacd960128a2fb6d68c40d2dfe6 to your computer and use it in GitHub Desktop.
Save dbasilioesp/1d723dacd960128a2fb6d68c40d2dfe6 to your computer and use it in GitHub Desktop.
int main()
{
int x;
ifstream infile;
infile.open("silly.dat", ios::binary | ios::in)
infile.read(&x, 7); // reads 7 bytes into a cell that is either 2 or 4
}
struct Person
{
char name[50];
int age;
char phone[24];
};
int main()
{
Person me = {"Robert", 28, "364-2534"};
Person book[30];
int x = 123;
double fx = 34.54;
ofstream outfile;
outfile.open("junk.dat", ios::binary | ios::out);
outfile.write(&x, sizeof(int)); // sizeof can take a type
outfile.write(&fx, sizeof(fx)); // or it can take a variable name
outfile.write(&me, sizeof(me));
outfile.write(book, 30*sizeof(Person))
outfile.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment