簡體   English   中英

編寫文本文件C ++

[英]Writing a text file c++

這是我的頭文件Person.hpp:

#ifndef PERSON_HPP
#define PERSON_HPP

#include <iostream>
#include <string>
using namespace std;

class Person {
public:
    Person(string name_val = "", int id_val = 0);
    virtual void write(ostream& strm);
    virtual void read(istream& strm);
private:
    string name;
    int id;
};
ostream& operator<<(ostream& strm, Person& person);
istream& operator>>(istream& strm, Person& person);

Person::Person(string name_val, int id_val)
: name(name_val), id(id_val) {

}

void Person::write(ostream& strm) {
    strm << id << " " << name;
}

void Person::read(istream& strm) {
    strm >> id >> name;
}

ostream& operator<<(ostream& strm, Person& person) {
    person.write(strm);
    return strm;
}

istream& operator>>(istream& strm, Person& person) {
    person.read(strm);
    return strm;
}

#endif

和第一個Student.hpp中的第二個頭文件:

#ifndef STUDENT_HPP
#define STUDENT_HPP

#include <iostream>
#include <iomanip>
#include <string>
#include "Person.hpp"
using namespace std;

class Student {
public:
    Student(string name_val = "", int id_val = 0, double gpa_val = 0.0);
    void write(ostream& strm);
    void read(istream& strm);
private:
    double gpa;
};
ostream& operator<<(ostream& strm, Student& student);
istream& operator>>(istream& strm, Student& student);

Student::Student(string name_val, int id_val, double gpa_val)
: gpa(gpa_val) {

}

void Student::write(ostream& strm) {
    //wait for modify
}

void Student::read(istream& strm) {
    //wait for modify too.
}

ostream& operator<<(ostream& strm, Student& student) {
    student.write(strm);
    return strm;
}

istream& operator>>(istream& strm, Student& student) {
    student.read(strm);
    return strm;
}

#endif

和main.cpp:

#include <iostream>
#include <fstream>
#include "Person.hpp"
#include "Student.hpp"
using namespace std;
int main() {
    Student aStudent;

    ofstream outFile;
    outFile.open("Student.txt");

    cout << "Enter ID and name, and grade point average" << endl;
    while (cin >> aStudent) {
        outFile << aStudent << endl;
        cout << "Enter ID and name, and grade point average" << endl;
    }
    outFile.close();
}

我想這樣收集我的數據:

Enter ID and name, and grade point average
5601 Bird 3.01
Enter ID and name, and grade point average
5602 Bee 2.58
Enter ID and name, and grade point average
5603 Bow 4.00
Enter ID and name, and grade point average
^Z (terminates)

程序將創建文本文件名稱student.txt,並在此文本文件中收集ID,名稱和GPA。 (很長的代碼,很抱歉。)

如何修改Student.hpp?

這很簡單。 您首先讀取/寫入基類( Person ),然后讀取/寫入特定於Student數據:

void Student::write(ostream& strm) {
    Person::write(strm);
    strm << gpa;
}

void Student::read(istream& strm) {
    Person::read(strm);
    strm >> gpa;
}

話雖如此,這可以在operator<<operator>> ,不需要read / write功能(除非您需要它們)。 另外,開始使用const關鍵字,例如:

write(ostream& strm) const;
//                   ^^^^^

ostream& operator<<(ostream& strm, Student const& student);
//                                         ^^^^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM