使用 getline 函数读取文件,但第一列显示为空

Reading a file with getline function, but first column appears empty

本文关键字:一列 显示 函数 getline 读取 文件 使用      更新时间:2023-10-16

我正在尝试读取文件并将读取的数据写入结构中。使用 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;
    }
  }
}