gpt4 book ai didi

c++ - 从 Excel 文件读取

转载 作者:行者123 更新时间:2023-11-30 02:36:13 30 4
gpt4 key购买 nike

我正在尝试从具有四列和 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;
}

关于c++ - 从 Excel 文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050350/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com