保存并加载c ++程序

时间:2014-04-23 23:55:07

标签: c++ serialization fwrite fread

好的,所以今天我想出了很多东西并且我想要感谢社区。我现在几个小时都没有碰到道路,但现在我已经卡住了。

路上最后一次撞击。保存并加载我的程序。我不知道从哪里开始。我看了fwrite ...和fread ...是如何工作的,所有的例子都是针对那些没有被拆分的程序。我不知道从哪里开始我的文件。我会提出2个功能。如果有人可以帮我怎么做,那么我可以解决剩下的问题。

在gradebook.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 ...来保存数据?

2 个答案:

答案 0 :(得分:2)

我不推荐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.

答案 1 :(得分:1)

这里有一些示例代码,如果不为您解决整个问题,可能会有所帮助。

#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));
}

如果你认识到我的学生和#34;是。 : - )