Pal*_*net 4 c++ csv excel fstream
我正在尝试读取包含 4 列和 121 行的 Excel 文件。我尝试了 .csv 的想法,但我想我理解错误,因为当我编译它时,它变得一团糟。
我如何确保 city 获得第一个单元格,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;
}
Run Code Online (Sandbox Code Playgroud)
尝试这个
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;
}
Run Code Online (Sandbox Code Playgroud)