C++ 如何从文件中填充类中的结构?

C++ 如何从文件中填充类中的结构?,c++,class,struct,C++,Class,Struct,我有一个赋值,我们需要创建一个类对象,它有许多不同的变量,其中一个是结构。我不知道如何从setter函数填充结构。我附加了一些从代码中提取的代码片段。我的count_file_line函数返回一个int值,即一个txt文件中有多少行。我对编码也很陌生,一直在努力,所以如果这是一个显而易见的答案,对不起 当我运行程序并尝试从setter函数中输入[I].密码时,没有显示任何内容(“标志”确实显示) struct教师{ int-id; 字符串密码; 字符串名; 字符串last_name; }; vo

我有一个赋值,我们需要创建一个类对象,它有许多不同的变量,其中一个是结构。我不知道如何从setter函数填充结构。我附加了一些从代码中提取的代码片段。我的count_file_line函数返回一个int值,即一个txt文件中有多少行。我对编码也很陌生,一直在努力,所以如果这是一个显而易见的答案,对不起

当我运行程序并尝试从setter函数中输入[I].密码时,没有显示任何内容(“标志”确实显示)

struct教师{
int-id;
字符串密码;
字符串名;
字符串last_name;
};
void University::set_教师(ifstream和infle){
int AMONTOFTACHERS=计数文件行(填充);
这->教师=新教师[教师人数];
for(int i=0;i>教师[i].密码;
教师[i].姓名;
教师[i].姓;

你想要完成的是一系列的教师目标

您可能对以下内容感兴趣:


对于一些通用解决方案。请注意,对于您需要实现的目标而言,这些可能(也可能不是)有点“沉重”。

使用tell的示例,不要问:

#include <iostream>
using std::cout, std::cerr, std::endl;

#include <iomanip>
using std::setw, std::setfill;

#include <fstream>
using std::ifstream, std::istream; // std::ofstream;

#include <sstream>
using std::stringstream;

#include <string>
using std::string, std::to_string;

#include <cstdint>

#include <cassert>


// stub - this function implemented and tested elsewhere
int count_file_lines(ifstream& inFile)
{
   if (!inFile.good())
      cerr << "\n  !infile.good()" << endl;
   return 5; // for test purposes
}


struct teacher
{
private:
   int    id;       // unique number in record order
   string password;
   string first_name;
   string last_name;
   static int ID;  // init value below

   // note: On my system each string is 32 bytes in this object,
   //       regardless of char count: the chars are in dynamic memory

public:
   teacher() : id(++ID) // password, first_name, last_name
      { }               //        default init is empty string

   ~teacher() = default; // do nothing

   void read(istream& inFile)  // tell instance to read next record
      {
         inFile >> password;
         inFile >> first_name;
         inFile >> last_name;
      }

   void show()
      {
         cout << "\n  show  id:" << id
              << "\n  pw      :" << password
              << "\n  fn      :" << first_name
              << "\n  ln      :" << last_name
              << endl;
      }

};

int teacher::ID = 0;  // compute unique ID number for each record

这不取决于生成文件的方式吗?…如何计算行数?在循环中使用
std::getline
并递增一个数字?是否使用
seekg()回放读取位置
在尝试再次读取文件或打开另一个
std::fstream
之前,没有其他学生询问向量,她说现在不要使用这些向量。行数函数工作正常,测试并在我的程序中的其他函数中使用,没有任何问题,因此这不是问题。我正在读取的txt文件是“password first\u name last\u name/n”格式。Yksisarvinen意味着
计数文件行
具有移动流光标位置的副作用。因此,完成后
填充
处于“文件结束状态”。进一步读取运算符不读取任何内容。首选“告诉,不要询问”。从告诉“不要问”是一个原则,它帮助人们记住面向对象是将数据与操作在数据上的函数捆绑在一起。它提醒我们,与其向对象请求数据和对数据进行操作,不如告诉对象应该做什么。“因此,考虑删除SETER函数(即StIGH教师)。,而是重构代码,将函数“read()”添加到struct teacher,然后使用“teacher::read()”告诉每个新的“teacher”对象从文件中读取其初始数据。请注意main()(或者在本例中,F834_t::exec())不需要知道有关教师或文件结构的任何信息。只需要知道有多少个对象,以及“read()”的函数思想和“show()”。teacher.read()更简单。teacher.show更简单。结构复杂性捆绑在类中…其他代码不需要知道这些复杂性。
#include <iostream>
using std::cout, std::cerr, std::endl;

#include <iomanip>
using std::setw, std::setfill;

#include <fstream>
using std::ifstream, std::istream; // std::ofstream;

#include <sstream>
using std::stringstream;

#include <string>
using std::string, std::to_string;

#include <cstdint>

#include <cassert>


// stub - this function implemented and tested elsewhere
int count_file_lines(ifstream& inFile)
{
   if (!inFile.good())
      cerr << "\n  !infile.good()" << endl;
   return 5; // for test purposes
}


struct teacher
{
private:
   int    id;       // unique number in record order
   string password;
   string first_name;
   string last_name;
   static int ID;  // init value below

   // note: On my system each string is 32 bytes in this object,
   //       regardless of char count: the chars are in dynamic memory

public:
   teacher() : id(++ID) // password, first_name, last_name
      { }               //        default init is empty string

   ~teacher() = default; // do nothing

   void read(istream& inFile)  // tell instance to read next record
      {
         inFile >> password;
         inFile >> first_name;
         inFile >> last_name;
      }

   void show()
      {
         cout << "\n  show  id:" << id
              << "\n  pw      :" << password
              << "\n  fn      :" << first_name
              << "\n  ln      :" << last_name
              << endl;
      }

};

int teacher::ID = 0;  // compute unique ID number for each record
class F834_t
{
   teacher* teachers = nullptr; // do not yet know how many
   ifstream inFile;             // declared, but not opened
   uint     amountOfTeachers = 0; 

   stringstream ss;     // for debug / demo use


public:
   // use default ctor, dtor
   F834_t() = default;
   ~F834_t() = default;

   int exec(int , char** )
      {
         // open infile to count lines

         amountOfTeachers = static_cast<uint>(count_file_lines(inFile)); // use working func
         cout << "\n  teacher count: " << amountOfTeachers << "\n ";   // echo

         // init ss with 5 values
         for (uint i=1; i<=amountOfTeachers; ++i)
            ss << " pw" << i << " fn" << i << " ln" << i << "  ";
         cout << ss.str() << endl;

         teachers = new teacher[amountOfTeachers]; // allocate space, invoke default ctor of each
         assert(teachers);
         cout << "\n     teachers: " << setw(4) << sizeof(teachers) << "  (pointer bytes)"
              << "\n    a teacher: " << setw(4) << sizeof(teacher)  << "  (teacher bytes)"
              << "\n  size of all: " << setw(4) << (amountOfTeachers * sizeof(teacher))
              << "  ( " << setw(3) << sizeof(teacher) << " * " <<  setw(3) << amountOfTeachers << ')'
              << endl;

         // reset stream to start of inFIle, maybe close/open inFile

         for (uint i=0;i<amountOfTeachers; ++i)
         {
            assert(ss.good()); // (inFile.good());

            teachers[i].read(ss); // (inFile);  // tell the object to read the file
         }

         for (uint i=0;i<amountOfTeachers; ++i)
         {
            teachers[i].show(); // tell the object to show its contents
         }

         return 0;
      }
}; // class F834_t


int main(int argc, char* argv[])
{
   F834_t f834;

   return f834.exec(argc, argv);
}
  teacher count: 5
  pw1 fn1 ln1   pw2 fn2 ln2   pw3 fn3 ln3   pw4 fn4 ln4   pw5 fn5 ln5  

     teachers:    8  (pointer bytes)
    a teacher:  104  (teacher bytes)
  size of all:  520  ( 104 *   5)

  show  id:1
  pw      :pw1
  fn      :fn1
  ln      :ln1

  show  id:2
  pw      :pw2
  fn      :fn2
  ln      :ln2

  show  id:3
  pw      :pw3
  fn      :fn3
  ln      :ln3

  show  id:4
  pw      :pw4
  fn      :fn4
  ln      :ln4

  show  id:5
  pw      :pw5
  fn      :fn5
  ln      :ln5