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

How to fill structs within a class from a file?

本文关键字:结构 填充 文件      更新时间:2023-10-16

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

当我运行程序并尝试从setter函数中定制教师[I].密码时,没有显示任何内容("Flag"确实显示(

struct teacher{
int id;
string password;
string first_name;
string last_name;
};
void University::set_teachers(ifstream& inFile){
int amountOfTeachers = count_file_lines(inFile);
this->teachers = new teacher[amountOfTeachers];
for(int i = 0; i < amountOfTeachers; i++){
inFile >> teachers[i].password;
inFile >> teachers[i].first_name;
inFile >> teachers[i].last_name;
cout << "Flag" << endl;
}
}

您试图完成的是对一系列教师对象的"反序列化"。

您可能对感兴趣

是否可以在C++中序列化和反序列化类?

用于某些通用解决方案。请注意,对于你需要实现的目标来说,这些可能有点"沉重"。

使用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

以及输入和输出的演示(教师::read((,教师::show(((

注意使用"字符串流ss;"。它使用for循环填充,并使用"tear.read(("传递给每个教师对象。

然后使用"teach.show(("将教师值回声输出

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