小编use*_*271的帖子

继承自ifstream

我可以从ifstream继承并从我的派生类中读取文件,如下所示:

#include <iostream>

using namespace std;

const string usage_str = "Usage: extract <file>";

class File: public ifstream
{
public:
    explicit File(const char *fname, openmode mode = in);
    void extract(ostream& o);
};

File::File(const char *fname, openmode mode)
{
    ifstream(fname, mode);
}

void File::extract(ostream& o)
{
    char ch;
    char buf[512];
    int i = 0;

    while (good()) {
        getline(buf, sizeof(buf));
        o<<buf;
        i++;
    }   
    cout<<"Done "<<i<<" times"<<endl;
}

void msg_exit(ostream& o, const string& msg, int exit_code)
{
    o<<msg<<endl;
    exit(exit_code);
}

int do_extract(int argc, char …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance ifstream

4
推荐指数
1
解决办法
3805
查看次数

标签 统计

c++ ×1

ifstream ×1

inheritance ×1