Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用getline函数读取文件,但第一列显示为空_C++ - Fatal编程技术网

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

C++ 使用getline函数读取文件,但第一列显示为空,c++,C++,我试图读取一个文件并将读取的数据写入一个结构。使用getline函数,我读取整行内容,然后将其划分为列。每一项进入结构的一个参数,每一行都是结构的一个新实例。问题是我的第一列是空的 下面的代码部分工作,我阅读了整个文件,所有其他列都工作得很好,但第一列没有填充任何内容 这是我的结构和我输入的数据: struct employe { int num_employe; string nom; string prenom; string date_naissance;

我试图读取一个文件并将读取的数据写入一个结构。使用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);
}
我从中提取数据的文件如下所示:


当我显示第二列(
cout时,我认为您应该像这样循环

while(std::getline(myfile, line, '\n'))
而不是

while (myfile >> temp)
这是在删除每行中的第一个单词…

单独使用getline()获取每行 无需使用此行:

while(myfile>>temp)

这是抓住第一个字,再也不会叫了

相反,通过直接在filestream上调用getline()在每一行上循环:

while(getline(myfile,line,'\n'))

我喜欢您使用stringstream来解析单词。我可能也会在saisir函数中使用stringstream来进行解析调用(例如,而不是stoi())

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构雇员{
国际雇员人数;
字符串名称;
字符串前缀;
字符串日期(诞生);;
弦乐村;
国际邮政编码;
};
员工saisir_1_员工(矢量行)
{
雇员;
e、 num_employe=stoi(第[0]行);
e、 nom=行[1];
e、 prenom=第[2]行;
e、 出生日期=第[3]行;
e、 ville_resi=第[4]行;
e、 邮政编码=stoi(第[5]行);
返回(e);
}
int main()
{
fstream myfile{“myfile.txt”};
std::字符串行;
字符串字;
病媒雇员;
如果(myfile.is_open()){
//while(myfile>>temp){
//
//row.clear();
//读一整行,然后
//将其存储在字符串变量“line”中
while(getline(myfile,line,'\n')){
std::向量行;
std::istringstream sline(线);
//读取一行的每一列数据,然后
//将其存储在字符串变量“word”中
while(getline(sline,单词'\t')){
//添加所有列数据
//将行转换为向量
行。推回(word);
}
雇员;
e=saisir_1_员工(世界其他地区);
员工。推回(e);
//雇主(e);
}
int指数=1;
//转储号和名称
适用于(康斯特汽车和emp:员工){

std::cout while(myfile>>temp)
的目的是什么?您不应该在while(getline(myfile,line,'\n'))
中执行此操作吗?按值返回局部变量是惯用方法,无需更改
while (myfile >> temp)
#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;
    }

  }
}