从Excel文件中读取

时间:2015-10-10 04:39:41

标签: c++ excel csv fstream

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

如何确保城市获得第一个单元格,国家获得第二个,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;
} 

1 个答案:

答案 0 :(得分:3)

试试这个

routes