使用 getline 函数读取文件,但第一列显示为空
Reading a file with getline function, but first column appears empty
我正在尝试读取文件并将读取的数据写入结构中。使用 getline 函数,我读取整行,然后将其分成列。每个项都进入结构的参数,每一行都是结构的新实例。问题是我的第一列是空的。
下面的代码部分工作,我阅读了整个文件,所有其他列都运行良好,但第一列什么都没有填充。
这是我的结构和我放入其中的数据:
struct employe {
int num_employe;
string nom;
string prenom;
string date_naissance;
string ville_resi;
int code_postal;
};
employe saisir_1_employe(vector<string> row) {
employe e;
e.num_employe = stoi(row[0]);
e.nom = row[1];
e.prenom = row[2];
e.date_naissance = row[3];
e.ville_resi = row[4];
e.code_postal = stoi(row[5]);
return (e);
}
我像这样从文件中提取数据:
if (myfile.is_open()) {
while (myfile >> temp) {
row.clear();
// read an entire row and
// store it in a string variable 'line'
getline(myfile, line, 'n');
istringstream s(line);
// read every column data of a row and
// store it in a string variable, 'word'
while (getline(s, word, 't')) {
// add all the column data
// of a row to a vector
row.push_back(word);
}
e = saisir_1_employe(row);
afficher_1_employe(e);
}
}
我从中提取数据的文件看起来像这样:https://pastebin.com/Nfhu2tEp
当我显示第二列(cout << row[1]
(时,我完美地得到了名称。但是当我这样做时cout << row[0]
我得到一个空列,当它应该是一个字符串时,我将其转换为带有 e.num_employe = stoi(row[0])
的 int .它在那里并且有正确的行数,但只是空的。
我认为你应该像这样循环
while(std::getline(myfile, line, 'n'))
而不是
while (myfile >> temp)
这是在削减每行中的第一个单词...
使用 getline(( 本身来获取每一行无需使用此行:
while(myfile >> temp)
这是抓住第一个单词,再也没有被叫过。
相反,通过直接在文件流上调用 getline(( 在每一行上循环:
while (getline(myfile, line, 'n'))
我喜欢你使用字符串流来解析单词。 我可能也会在 saisir 函数中使用字符串流来执行解析调用(例如,而不是 stoi(((。
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct employe {
int num_employe;
string nom;
string prenom;
string date_naissance;
string ville_resi;
int code_postal;
};
employe saisir_1_employe(vector<string> row )
{
employe e;
e.num_employe = stoi(row[0]);
e.nom = row[1];
e.prenom = row[2];
e.date_naissance = row[3];
e.ville_resi = row[4];
e.code_postal = stoi(row[5]);
return (e);
}
int main()
{
fstream myfile{"myfile.txt"};
std::string line;
std::string word;
std::vector<employe> employees;
if (myfile.is_open()) {
// while (myfile >> temp) {
//
// row.clear();
// read an entire row and
// store it in a string variable 'line'
while (getline(myfile, line, 'n')) {
std::vector<std::string> row;
std::istringstream sline(line);
// read every column data of a row and
// store it in a string variable, 'word'
while (getline(sline, word, 't')) {
// add all the column data
// of a row to a vector
row.push_back(word);
}
employe e;
e = saisir_1_employe(row);
employees.push_back(e);
// afficher_1_employe(e);
}
int index = 1;
// dump number and nom
for(const auto& emp : employees) {
std::cout << index++ << ". " << emp.num_employe
<< " " << emp.nom << std::endl;
}
}
}
相关文章:
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 如何在C++中获取二维数组中最少的一列数?
- 如何允许用户选择何时停止三列中的每一列的旋转
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- QTableWidget一次显示一定数量的行
- 在犰狳立方体中添加一列 1 的有效方法
- 在二维向量的每一列中查找最大值
- 只有一列适合其内容大小,而另一列则拉伸
- 如何将单独的数组分组在一起(类似于按一列对表进行排序)
- 使用 getline 函数读取文件,但第一列显示为空
- C++ 最后一列中的直方图错误
- 添加两个矩阵会打印一列垃圾数据 c++
- C 特征:通过参考将矩阵的一列传递到函数中
- 仅制作一列qtreewidget可编辑//故障排除
- 在 QTableView 中仅对一列进行排序,而不对其他列进行排序.独立列
- 按列对CSV文件进行排序,并与C 中的另一列进行比较
- 隔离C 中向量的一列
- 读取数据文件并将每一列分配给各个数组
- 按一列对二维数组进行气泡排序
- 可能的话,将QComboBox映射到MySQL表的一列