如何在C ++中实现我自己的IO文件API [英] How to implement my own IO file API in C++

查看:157
本文介绍了如何在C ++中实现我自己的IO文件API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我自己的输入/输出文件API。为什么我要这样做的原因是在低级别会有很多文件系统(硬盘,HTTP等等),我需要为用户提供一个通用的接口。
这个API看起来有点像这样:

pre codelass $ G $ $
$ 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;
//多得多
};

所以现在我正在执行硬盘内存,这是最简单的。我想这样做的方法是保持指向当前使用的文件的ifstream和ofstream指针。所以我想在Open函数中使用我的ifstream或者ofstream指针(取决于OpenMode)指向打开的文件,然后在write或者read函数中对这个fstream进行操作。 有没有更好的主意?

编辑:以下问题解决



无论如何,当在ifstream和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; **这导致编译错误**

错误是:missing;在标识符infile之前。类型说明符 - int假定。



我该如何解决这个问题?

解决方案

不要忘记,在C ++标准库头中定义的类型是在 std 命名空间中定义的:

  std :: ifstream infile; 


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 ?

解决方案

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

std::ifstream infile;

这篇关于如何在C ++中实现我自己的IO文件API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆