c++ - Reading from an Excel file -
i trying read excel file has 4 columns , 121 rows. tried .csv idea, guess understood wrong because when compile this, gets messed up.
how can make sure city gets first cell, country gets second, lon gets third, , lat gets fourth?
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream infile; string city; string country; string lat; string lon; infile.open("worldcities.csv"); if (infile.is_open()) { cout << "file has been opened" << endl; } else { cout << "no file has been opened" << endl; } while (!infile.eof()) { infile >> city; infile >> country; infile >> lon; infile >> lat; cout << "city: " << city << endl; cout << "country: " << country << endl; cout << "lat: " << lat << endl; cout << "lon: " << lon << endl; } infile.close(); system("pause"); return 0; }
try this
while (!infile.eof()) { getline ( infile, city, ',' ); getline ( infile, country, ',' ); getline ( infile, lat, ',' ); infile >> lon; cout << "city: " << city << endl; cout << "country: " << country << endl; cout << "lat: " << lat << endl; cout << "lon: " << lon << endl; }
Comments
Post a Comment