How to implement my own IO file API in C++

人走茶凉 提交于 2019-12-11 03:38:04

问题


I want to implement my own Input/Output file API. The reason for why I want to that is that on the Low level there will be many file systems (hard disk, HTTP and some more) and I need to provide an common interface for user. The API looks somewhat like this :

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
};

So right now I am implementing Hard disk memory, which is the easiest. The way I want to do it is to keep an ifstream and ofstream pointer to the current file which is used. So I want in Open function use my ifstream or ofstream pointer (depending on OpenMode) to point to opened file, and then in write or read function operate on this fstream. Are there any better ideas to do that ?

EDIT: PROBLEM BELOW RESOLVED

Anyway I am getting compilation error when adding ifstream, and ofstream in the header:

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**

And the error is : "missing ; before identifier infile. missing type specifier - int assumed.

How Can I resolve this ?


回答1:


Don't forget that types defined in the C++ standard library headers are defined in the std namespace:

std::ifstream infile;



回答2:


#include <fstream>
using namespace std;

will probably fix your problem



来源:https://stackoverflow.com/questions/15681574/how-to-implement-my-own-io-file-api-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!