保存并加载c++程序

save and load c++ program

本文关键字:程序 c++ 加载 保存      更新时间:2023-10-16

好的,所以我今天发现并学到了很多东西,我想为此感谢社区。我已经几个小时没有在路上颠簸了,但现在我被卡住了。

路上的最后一个颠簸。保存并加载我的程序。我不知道从哪里开始。我看着如何写作。。。和fread。。。工作,所有的例子都是针对未拆分的程序。我不知道从哪里开始处理我的文件。我将设置两个功能。如果有人能帮我如何拯救那些人,我可能会想出剩下的办法。

在成绩册.h 中

class Student {
 public: 
  string last;
  string first;
  int student_id;
};
class Course {
 public:
  string name;
  int course_id;
  vector <Student> students;
};
class Gradebook {
 public:
  Gradebook();
  void addCourse();
  void addStudent();
 private:
  vector <Course> courses;
};

在gradebook.cpp 中

void Gradebook::addCourse() {
 int i, loop=0;
    cout << "Enter Number of Courses: ";
cin >> loop;
for(i=0; i<loop; i++) {
    //create newEntry to store variables
    Course newEntry;
    cout << "Enter Course ID: ";
    cin >> newEntry.course_id;
    cout << "Enter Course Name: ";
    cin >> newEntry.name;
    //set variables from newEntry in Courses
    courses.push_back(newEntry);
}
}
void Gradebook::addStudent() {
    int i, loop=0;
cout << "Enter Number of Students: ";
cin >> loop;
for(i=0; i<loop; i++) {
    //create newEntry to store variables
    Student newEntry; 
    cout << "Enter Student ID: ";
    cin >> newEntry.student_id;
    cout << "Enter Last Name: ";
    cin >> newEntry.last;
    cout << "Enter First Name: ";
    cin >> newEntry.first;
    //set variables from newEntry in Students
    courses[0].students.push_back(newEntry);
}
}

所以,如果用户在课程和学生中输入一些变量,我将如何使用fwrite。。。保存数据?

我不推荐fwrite,而是研究<fstream>ifstreamofstream

基本保存:

ofstream out("data.txt"); //save file data.txt
out << thedata; //use the << operator to write data

基本装载:

ifstream in("data.txt"); //reopen the same file
in >> thedata; //use the >> operator to read data.

以下是一些示例代码,这些代码可能会在不解决整个问题的情况下有所帮助。

#include<iostream>
#include<string>
#include<vector>
#include<fstream>
class Student {
public: 
    Student()
    : student_id(0)
    {
    }
    Student(const std::string &f, const std::string &l, int id)
        : first(f)
        , last(l)
        , student_id(id)
    {
    }
    std::string last;
    std::string first;
    int student_id;
};
std::ostream &operator <<(std::ostream &os, const Student &s)
{
    os << s.last << 't' 
       << s.first << 't' 
       << s.student_id << 't';
    return os;
}
std::istream &operator >>(std::istream &is, Student &s) 
{
    is >> s.last
       >> s.first
       >> s.student_id;
    return is;
}

bool WriteIt(const std::string &sFileName)
{
    std::vector<Student> v;
    v.push_back(Student("Andrew", "Bogut",   1231));
    v.push_back(Student("Luc",    "Longley", 1232));
    v.push_back(Student("Andrew", "Gaze",    1233));
    v.push_back(Student("Shane",  "Heal",    1234));
    v.push_back(Student("Chris",  "Anstey",  1235));
    v.push_back(Student("Mark",   "Bradtke", 1236));
    std::ofstream os(sFileName);
    os << v.size();
    for (auto s : v)
        os << s;
    return os.good();
}
bool ReadIt(const std::string &sFileName)
{
    std::ifstream is(sFileName);
    int nCount(0);
    is >> nCount;
    if (is.good())
    {
        std::vector<Student> v(nCount);
        for (int i = 0; i < nCount && is.good(); ++i)
            is >> v[i];
        if (is.good())
            for (auto s : v)
                std::cout << s << std::endl;
    }
    return is.good();
}
int main()
{
    const std::string sFileName("Test.dat");
    return !(WriteIt(sFileName) && ReadIt(sFileName));
}

如果你认出我的"学生"是谁,就可以获得加分。:-)