2014-04-23 2 views
1

좋아, 그래서 알아 냈어, 오늘 많은 것들을 배웠고, 나는 그 지역 사회에 감사 드리고 싶다. 나는 2,3 시간 동안 길에서 조금의 충돌도 가지지 않고 있었다. 그러나 지금 나는 붙이게된다.저장 및로드 C++ 프로그램

도로의 마지막 범프. 내 프로그램 저장 및로드. 어디서부터 시작해야할지 모르겠습니다. fwrite와 fread가 어떻게 작동하는지 살펴 봤는데 모든 예제는 분할되지 않은 프로그램을위한 것입니다. 내 파일로 어디서부터 시작해야할지 모르겠다. 2 가지 기능을 추가하겠습니다. 누군가가 나를 구하는 방법을 도와 줄 수 있다면 나는 나머지를 알아낼 수있을 것입니다. gradebook.cpp에서 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; 
}; 

에서

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>을 살펴보십시오. ifstream, ofstream

기본 절약 :

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

어떻게 스트림이 ... 어떤 정보를 알아야하는지 알고 있습니까? – 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)); 
} 

내 "학생"이 누구인지 알고있는 경우 보너스 포인트. :-)