Пример использования скажи, не спрашивай:
#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
и демонстрация ввода и вывода (teacher :: read (), teacher :: show ())
Обратите внимание на использование «stringstream ss;». Он заполняется с помощью цикла for и передается каждому объекту учителя с помощью «teacher.read ()».
Затем значения учителя выводятся с помощью «teacher.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