-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryOut++.cpp
More file actions
63 lines (53 loc) · 1.43 KB
/
binaryOut++.cpp
File metadata and controls
63 lines (53 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Purpose: binary file output example
* Author: Emanuele Rizzolo
* Class: 3XIN
* Date: 2020/04/26
* Note:
*/
// directive for standard io functions
#include <iostream>
// directive for file io functions
#include <fstream>
using namespace std;
// planet definition and data
#include "planet.h"
#define DEBUG 1
void print(const char title[], const planet planets[], int numPlanets);
// main function
int main(int argc, char *argv[])
{
print("Planets to be written:", planets, numPlanets);
// filename
const char filename[] = "planets++.dat";
// open the file for output
// automatic open with explicit open mode out and binary
ofstream outfile(filename, ios::out | ios::binary);
// check failure
if (outfile)
{
// I/O operation
outfile.write((char *)planets, sizeof(planets));
if (DEBUG)
{
cout << outfile.tellp() / sizeof(planet) << " planets written on " << filename << endl;
}
// close file
outfile.close();
}
else
{
cout << "failed to open file " << filename << endl;
}
// successful termination
return 0;
}
void print(const char title[], const planet planets[], int numPlanets)
{
cout << title << endl;
for (int p = 0; p < numPlanets; p++)
{
cout << planets[p] << endl;
}
cout << endl;
}