我想实现自己的输入/输出文件API。我之所以想要这样做,是因为在低级别上将有许多文件系统(硬盘,HTTP等),并且我需要为用户提供一个通用接口(interface)。
API看起来像这样:
class GIO_CORE
{
public:
GIO_CORE(void);
virtual int open(char* path, int openMode) = 0;
virtual int close() = 0;
virtual void write(char* s, int size) = 0;
virtual void read(char* s, int size) = 0;
//many more
};
因此,现在我正在实现最简单的硬盘存储器。我要这样做的方法是保持一个ifstream和ofstream指针指向所使用的当前文件。所以我想在Open函数中使用我的ifstream或ofstream指针(取决于OpenMode)指向打开的文件,然后在write或read函数中对此fstream进行操作。 有更好的主意吗?
编辑:问题已解决
无论如何,我在标题中添加ifstream和ofstream时遇到编译错误:
class GIO_Persistent_File_System : public GIO_CORE
{
public:
GIO_Persistent_File_System(void);
int open(char*, int);
int close();
void write(char* s, int size);
void read(char* s, int size);
ifstream infile; **this causes compilation error**
错误是:“缺少;标识符infile前。缺少类型说明符-假定为int。
我该如何解决?
最佳答案
不要忘记,在C++标准库 header 中定义的类型是在std
命名空间中定义的:
std::ifstream infile;