C++ 在c+中读取CSV文件中的两列+;
我有一个CSV文件,分为两列:姓名、年龄 为了阅读和储存这些信息,我做了这个C++ 在c+中读取CSV文件中的两列+;,c++,csv,C++,Csv,我有一个CSV文件,分为两列:姓名、年龄 为了阅读和储存这些信息,我做了这个 struct person { string name; int age; } person record[10]; ifstream read("....file.csv"); 然而,当我 read >> record[0].name; read.get(); read >> record[0].age; 阅读>>名字给了我整行,而不仅仅是名字。我怎样才能避免这个问题,以便
struct person
{
string name;
int age;
}
person record[10];
ifstream read("....file.csv");
然而,当我
read >> record[0].name;
read.get();
read >> record[0].age;
阅读>>名字给了我整行,而不仅仅是名字。我怎样才能避免这个问题,以便将整数读入年龄
谢谢大家!
read>>name
给了我整行,而不仅仅是名字。我怎样才能避免这个问题,以便将整数读入年龄
read>>name
将所有内容读取到name
中,直到遇到空白
如果有一行以逗号分隔,不带空格,则可以将整行读入name
可以使用将整行读取为一个字符串。然后使用各种方法标记std::string
示例SO发布地址标记化std::string
:
你也许可以使用stringstreams,但老实说,我不相信这一点。 如果我是你,我会写一个小函数,将整行内容读入字符串,然后在字符串中搜索分隔符。前面的都是第一列,后面的都是第二列。通过C++提供的字符串操作,可以将这些部分移到变量中(如果需要,可以将它们转换成正确类型)。 我为CSV解析编写了一个小型C++库,也许看看它对你有帮助。你可以在电视上找到它 编辑:
在此要点中,您可以找到,您可以首先阅读整行内容,然后通过(must
#include
)解析它,如
下面是一个标记CSV文件的完整通用示例
#包括
#包括
#包括
#包括
#包括
int main()
{
//在你的情况下,你会有一个文件
//std::ifstream ifile(“input.txt”);
std::stringstream ifile(“User1,21,70\nUser2,25,68”);
std::string line;//我们在这里读了整行
while(std::getline(ifile,line))//读取当前行
{
std::istringstream iss{line};//从第行构造字符串流
//从当前行中读取以逗号分隔的标记
std::vector tokens;//我们在这里存储这些标记
std::string-token;//当前令牌
while(std::getline(iss,token,,'))
{
tokens.push_back(token);//将标记添加到向量
}
//我们现在可以处理代币了
//首先显示它们
std::非常感谢vsoftco。你介意再解释一下吗?假设我有三列,字符串名称,int-age,int-height。代码是什么样子的?@biolorgetline
部分只是“吃”流一直到逗号,然后忽略逗号。在这种情况下,您需要另一个getline,使用std::stoi
将读取的字符串转换为整数,并记录结果。最后,最后一次读取可以是iss>>record[0]。height
。一般情况类似于:while(getline(token,,')){//处理字符串标记}
你好,vsoftco,对不起,我没有得到令牌部分。你介意用完整的解决方案替换你当前的答案吗?非常感谢!@Digoro我在答案中添加了这个示例,因为我意识到其他人可能会觉得它很有用。非常感谢你的帮助。我只是将我的代码作为一个单独的问题发布,因为我无法将所有内容都作为注释放在这里。你介意帮我吗?我不明白为什么失败了。
std::string line;
while (std::getline(read, line)) // read whole line into line
{
std::istringstream iss(line); // string stream
std::getline(iss, record[0].name, ','); // read first part up to comma, ignore the comma
iss >> record[0].age; // read the second part
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
// in your case you'll have a file
// std::ifstream ifile("input.txt");
std::stringstream ifile("User1, 21, 70\nUser2, 25,68");
std::string line; // we read the full line here
while (std::getline(ifile, line)) // read the current line
{
std::istringstream iss{line}; // construct a string stream from line
// read the tokens from current line separated by comma
std::vector<std::string> tokens; // here we store the tokens
std::string token; // current token
while (std::getline(iss, token, ','))
{
tokens.push_back(token); // add the token to the vector
}
// we can now process the tokens
// first display them
std::cout << "Tokenized line: ";
for (const auto& elem : tokens)
std::cout << "[" << elem << "]";
std::cout << std::endl;
// map the tokens into our variables, this applies to your scenario
std::string name = tokens[0]; // first is a string, no need for further processing
int age = std::stoi(tokens[1]); // second is an int, convert it
int height = std::stoi(tokens[2]); // same for third
std::cout << "Processed tokens: " << std::endl;
std::cout << "\t Name: " << name << std::endl;
std::cout << "\t Age: " << age << std::endl;
std::cout << "\t Height: " << height << std::endl;
}
}