-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvOut++.cpp
More file actions
67 lines (58 loc) · 1.59 KB
/
csvOut++.cpp
File metadata and controls
67 lines (58 loc) · 1.59 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
64
65
66
67
/**
* Purpose: text file CSV output example, C++ style
* Author: Emanuele Rizzolo
* Class: 3XIN
* Date: 2020/04/26
* Note: CSV stands for Comma Separated Values
*/
// directive for standard io functions
#include <iostream>
// directive for io manipulation
#include <iomanip>
// directive for file io functions
#include <fstream>
using namespace std;
#include "planet.h"
#define DEBUG 0
const bool header = true; // whether to use an header line
const char delim = '"'; // string delimiter (usually ")
const char escape = '\\'; // escape character
void printCSV(const planet &p, ostream &os)
{
// output planet p on os
os << quoted(p.name, delim, escape);
os << "," << p.mass;
os << "," << p.distance;
os << "," << p.inhabited;
os << "," << p.numSatellites;
os << endl;
}
// main function
int main(int argc, char *argv[])
{
char filename[] = "planets++.csv";
// open the file for output
ofstream outfile(filename); // automatic open with default open mode out
// check failure
if (outfile) // or if (outfile.is_open())
{
// success
// I/O operation
if (header)
{
outfile << "name,mass,distance,inhabited,numSatellites" << endl;
}
for (int p = 0; p < numPlanets; p++)
{
printCSV(planets[p], outfile);
}
// close file
outfile.close();
}
else
{
cout << "failed to open file " << filename << endl;
}
// successful termination
return 0;
}