-
Notifications
You must be signed in to change notification settings - Fork 0
/
29-csv_files.cpp
31 lines (20 loc) · 902 Bytes
/
29-csv_files.cpp
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
// Read a csv file
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream ip("data.csv"); // Read-in the input file
if(!ip.is_open()) cout << "Error: File Open" << "\n"; // Check if the file exists
string firstName;
string lastName;
string age;
while(ip.good()){ // Read until all data is processed
getline(ip, firstName, ',');
getline(ip, lastName, ',');
getline(ip, age, '\n');
cout << "Name: " << firstName << " " << lastName << "\n";
cout << "Age: " << age << "\n";
}
ip.close(); // Close the file
return 0;
}