-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvIn++.cpp
More file actions
85 lines (76 loc) · 2.41 KB
/
csvIn++.cpp
File metadata and controls
85 lines (76 loc) · 2.41 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Purpose: text file CSV input 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>
// directive for string io functions
#include <sstream>
// directive for string limits functions
#include <limits>
// directive for C-style string functions
#include <cstring>
using namespace std;
#include "planet.h"
#define DEBUG 1
const bool header = true; // whether to use an header line
const char delim = '"'; // string delimiter (usually ")
const char escape = '\\'; // escape character
#define BUFFER_SIZE 100
bool readCSV(const char line[], planet &p)
{
static auto ignoreable = numeric_limits<streamsize>::max();
istringstream in(line);
string name;
in >> quoted(name, delim, escape);
strcpy(p.name, name.c_str());
in.ignore(ignoreable, ',') >> p.mass; // ignore , and read
in.ignore(ignoreable, ',') >> p.distance; // ignore , and read
in.ignore(ignoreable, ',') >> p.inhabited; // ignore , and read
in.ignore(ignoreable, ',') >> p.numSatellites; // ignore , and read
return !(in.bad() || in.fail());
}
// main function
int main(int argc, char *argv[])
{
char filename[] = "planets++.csv";
// open it for input
// ifstream infile; // declaration
// infile.open(filename); // open
ifstream infile(filename); // automatic open with default open mode in
// ifstream infile(filename, ios::in); // automatic open with explicit open mode in
// check failure
if (infile.is_open()) // or simply if(infile)
{
char line[BUFFER_SIZE];
if (header)
{
infile.ignore(numeric_limits<streamsize>::max(), '\n'); // skip header line
}
while (infile.getline(line, BUFFER_SIZE))
{
if (DEBUG)
{
cout << line << endl;
}
planet p;
if (readCSV(line, p))
{
cout << p << endl;
}
}
}
else
{
cout << "failed to open file " << filename << endl;
}
// successful termination
return 0;
}