从Excel文件读取

我正在尝试从具有四列和121行的Excel文件读取。 我试过.csv的想法,但我想我理解错了,因为当我编译这个时,它会变得混乱起来。

如何确保城市获得第一个单元格,国家获得第二,第三个获得第三个,第四个获得第四个?

#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; } 

尝试这个

 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; }