从 Excel 文件读取

Reading from an Excel file

我正在尝试从具有四列和 121 行的 Excel 文件中读取。我尝试了 .csv 的想法,但我想我理解错了,因为当我编译它时,它变得一团糟。

如何确保 city 获得第一个 cell,country 获得第二个,lon 获得第三个,lat 获得第四个?

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