4

我可以从 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 *argv[])
{
    cout<<"Opening "<<argv[1]<<endl;
    File f(argv[1]);
    if (! f)
        msg_exit(cerr, usage_str, 1);
    f.extract(cout);
    return 0;
}

int main(int argc, char *argv[])
{
    if (argc < 2)
        msg_exit(cout, usage_str, 0);

    do_extract(argc, argv);
    return 0;
}

我希望它读取整个文件,但它只读取一个符号(这不是给定文件的第一个符号)......

4

3 回答 3

7

不要从 ifstream 继承。如果您需要更改输入流的行为,请从streambuf继承,然后istream围绕它构造一个。如果您只想添加助手,请将它们设为全局,以便您可以在任何 istream 上使用它们。

也就是说,您的错误在 File 构造函数中:

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

这构造了一个(未命名的)ifstream,然后立即关闭它。你想调用超类构造函数:

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

}
于 2011-01-24T09:49:28.267 回答
1

我没有看到您的提取功能有问题,但我不明白从 ifstream 派生的意义。

从一个类派生的目的是覆盖它的虚方法,这样当有人将 istream& 或 ifstream& 传递给一个函数(通常是 operator>>)时,你的覆盖就会被调用。

与 STL 集合不同,流确实使用层次结构和 v-tables,但新手通常会误解这个概念。

例如,如果您想更改它使用的缓冲区类型,您可以从 basic_streambuf 派生并使用附加了 streambuf 的简单 istream 或 ostream 对象。

更改打印或读取对象的方式不是通过从 iostream 或 streambuf 派生新类来实现的,也不能扩展 iomanips,您必须在流式传输的对象周围使用包装类。

于 2011-01-24T09:54:43.070 回答
0

您缺少对基本构造函数的调用。我想你的意思是:

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

}

而不是这个:

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

现在您可能正在读取一些未初始化内存的内容。第二个(当前)代码只是在堆栈上创建一个新的 ifstream 实例并立即销毁它。

于 2011-01-24T09:47:20.817 回答