2014-04-23 49 views
1

好吧,我想通了,今天學到了很多東西,我想爲此感謝社區。我現在幾個小時都沒有在路上碰到任何東西,但現在我被卡住了。保存並加載C++程序

路上的最後一個碰撞。保存並加載我的程序。我不知道從哪裏開始。我研究了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

我不會推薦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. 
+0

ofstream怎麼知道需要什麼信息出去? – user2816227

+0

你告訴它!我不知道你想輸出什麼。用您想要輸出的內容替換'thedata'。像'cout'一樣工作 – yizzlez

+0

您必須重載全球運營商<< – dsi

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

如果您認出我的「學生」是誰,可以獲得獎勵積分。 :-)