读入一个 excel 文件到 struct c++
Reading a excel file in to struct c++
我想将 excel 文件读入 structure.But 问题是它读取整个 excel 列 line.I 需要第一列中的数据one.Using fstreams 我打开了一个包含许多列的文件
我尝试了它 have.But 它显示了整个列的所有详细信息,如下所示
Name : Name,Nick
Nick : name,Phone
Phone : number,Carrier,Address
Carrier : Yashodhara,Yash,711256677,Mobitel,"No.
Address : 29,Bollatha,Ganemulla"
-----------------------
Name : Madushani,Madu,711345678,Mobitel,"No.
Nick : 12,
Phone : Gampaha"
Carrier : Sadeepa,Sad,789002264,Hutch,"No.
Address : 123,
-----------------------
我用下面的代码试过了。
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//structure for store contact details
struct contacts {
string name;
string nickName;
string phoneNumber;
string carrier;
string address;
};
int main() {
const int LIMIT = 10;
contacts limit[LIMIT];
ifstream file;
file.open("Contact.csv");
if (file) {
while (!(file.eof())) {
for (int i = 0; i <= 7; i++) {
file >> limit[i].name;
file >> limit[i].nickName;
file >> limit[i].phoneNumber;
file >> limit[i].carrier;
file >> limit[i].address;
}
}
}
else {
cout << "Error";
}
// Using the following to debug output
for (int i = 0; i < 7; i++) {
cout << "Name : " << limit[i].name << endl
<< "Nick : " << limit[i].nickName << endl
<< "Phone : " << limit[i].phoneNumber << endl
<< "Carrier : " << limit[i].carrier << endl
<< "Address : " << limit[i].address << endl
<< "-----------------------" << endl;
}
return 0;
}
我想知道如何以正确的顺序将上述详细信息阅读到结构中。
您可以使用std::getline()
string readString;
if (file) {
while (!(file.eof())) {
getline(file,readString); // Read the column headers
for (int i = 0; i <= 7; i++) {
getline(file,limit[i].name,','); // ',' is the separator
getline(file,limit[i].nickName,',');
getline(file,limit[i].phoneNumber,',');
getline(file,limit[i].carrier,',');
getline(file,limit[i].address); // Read until the end of the line
}
}
}
我不得不说我没有测试它,所以它可能需要一些安排。
我想将 excel 文件读入 structure.But 问题是它读取整个 excel 列 line.I 需要第一列中的数据one.Using fstreams 我打开了一个包含许多列的文件
我尝试了它 have.But 它显示了整个列的所有详细信息,如下所示
Name : Name,Nick
Nick : name,Phone
Phone : number,Carrier,Address
Carrier : Yashodhara,Yash,711256677,Mobitel,"No.
Address : 29,Bollatha,Ganemulla"
-----------------------
Name : Madushani,Madu,711345678,Mobitel,"No.
Nick : 12,
Phone : Gampaha"
Carrier : Sadeepa,Sad,789002264,Hutch,"No.
Address : 123,
-----------------------
我用下面的代码试过了。
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//structure for store contact details
struct contacts {
string name;
string nickName;
string phoneNumber;
string carrier;
string address;
};
int main() {
const int LIMIT = 10;
contacts limit[LIMIT];
ifstream file;
file.open("Contact.csv");
if (file) {
while (!(file.eof())) {
for (int i = 0; i <= 7; i++) {
file >> limit[i].name;
file >> limit[i].nickName;
file >> limit[i].phoneNumber;
file >> limit[i].carrier;
file >> limit[i].address;
}
}
}
else {
cout << "Error";
}
// Using the following to debug output
for (int i = 0; i < 7; i++) {
cout << "Name : " << limit[i].name << endl
<< "Nick : " << limit[i].nickName << endl
<< "Phone : " << limit[i].phoneNumber << endl
<< "Carrier : " << limit[i].carrier << endl
<< "Address : " << limit[i].address << endl
<< "-----------------------" << endl;
}
return 0;
}
我想知道如何以正确的顺序将上述详细信息阅读到结构中。
您可以使用std::getline()
string readString;
if (file) {
while (!(file.eof())) {
getline(file,readString); // Read the column headers
for (int i = 0; i <= 7; i++) {
getline(file,limit[i].name,','); // ',' is the separator
getline(file,limit[i].nickName,',');
getline(file,limit[i].phoneNumber,',');
getline(file,limit[i].carrier,',');
getline(file,limit[i].address); // Read until the end of the line
}
}
}
我不得不说我没有测试它,所以它可能需要一些安排。