读取Excel文件

Reading from an Excel file

本文关键字:文件 Excel 读取      更新时间:2023-10-16

我试图从一个Excel文件,有四列和121行读取。我尝试了。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;
}