C / C ++中的简单虚拟文件系统 [英] Simple Virtual Filesystem in C/C++

查看:133
本文介绍了C / C ++中的简单虚拟文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个非常简单的虚拟文件系统(VFS),它支持一些基本的文件系统操作,如fwrite,
fopen,fput等等。VFS是一个具体操作系统的抽象层。现在假设
fopen接口看起来像这样

  FILE VFS_File_Open(const unsigned char * strFile,int flags); 

现在我想知道如何在实际的实现这个接口的区别
filesystem我在说话。有C的东西告诉我应用程序在哪个操作系统上运行,这样
我可以这样做:

  FILE VFS_File_Open(const unsigned char strFile,int flags)
{
int OS = getOSID();

if(0S == 1)
//在这里执行在WIN操作系统上打开文件所需的系统调用
else if(OS == 2)
//在这里实现在Linux操作系统上打开文件所需的系统调用

}


b $ b

编辑:



感谢您的答案,这对于澄清一些事情非常有帮助!



现在我想知道是否有人知道我在哪里可以找到Windows文件oeprations的系统调用?很容易找到他们为Linux,但我struggeled找到类似的窗口,例如。我会感兴趣的系统调用打开一个文件,写一个文件等。



另一个注意事项:C stdio.h提供了多个stand IO操作

  FILE * fopen(const char * filename,const char * opentype)



换句话说,我不需要在我的VFS中重新实现fopen例程,因为Gnu C库会关心它正在处理的操作系统。那对吧?我只需要实现
功能,不支持stdio库,例如。



感谢

解决方案

也许最简单/最干净的实现将是创建两个单独的库,一个用于windows和一个用于linux,不会乱丢你的代码与级联如果切换语句。



此外,请记住您的支票内的代码 OS == something

code>将在所有系统上编译和链接,因此例如在linux上编译的库应该能够解析windows系统调用...



我认为分离东西(不同的操作系统,不同的cpp文件)是最简单的解决方案。 / p>

EDIT



如果您使用的是C ++, a href =http://www.cplusplus.com/reference/iostream/ =nofollow> streams ?标准库已经提供了您尝试实现的功能,并且在所有平台上都可用。

否则,这里有一个指向 Windows文件管理功能



/ strong>:



如果您想要一个跨平台文件系统库,支持目录创建,可以检查 boost文件系统库


I would like to implement a very simple Virtual Filesystem (VFS) which supports some basic file system operation like fwrite, fopen, fput, etc. The VFS is an abstraction layer on top of some concrete OS, e.g. Windows, Linux etc. Assume now, that the fopen interface looks something like this

FILE VFS_File_Open( const unsigned char* strFile, int flags );

Now I am wondering how I can make in the actual implementation of this interface the distinction about which filesystem I am talking to. Is there in C something that tells me on which OS the application is running so that I could do something like this:

FILE VFS_File_Open( const unsigned char strFile, int flags )
{
int OS = getOSID();

if (0S == 1)
  //implement here the system calls required to open a file on a WIN OS
else if (OS == 2)
  //implement here the system calls required to open a file on a Linux OS
etc  
}

EDIT:

Thanks so far for your answer gents, that was really helpful to clarify some things!

Now I am wondering if anyone knows where I can find the system calls for file oeprations for windows? It is easy to find them for Linux but I struggeled to find something similar for windows, e.g. I would be interested in the system calls to open a file, write a file, etc.

On another note: The C stdio.h offers a number of stand IO operations like

FILE * fopen (const char *filename, const char *opentype)

In other words, I do not have to reimplement the fopen routine in my VFS as the Gnu C library takes care about what OS it is dealing with, is that right? I just have to implement functionaliy that is not supported by stdio library, e.g. creating directories, which differ from filesystem to filesystem?

Thanks

解决方案

Maybe the simplest/cleanest implementation would be to create two separate libraries, one for windows and one for linux, without littering your code with cascaded if and switch statements. The libraries would implement the same functions, defined in a common header.

Also, remember that the code inside your check OS == something would be compiled and linked on all systems, so e.g. the library compiled on linux should be able to resolve the windows system calls...

I think separating things (different OS, different cpp files) is the simplest solution.

EDIT:

If you are using C++, why not just relying on streams? The standard library already provides the functionality you are trying to implement and is available on all platforms.
Otherwise, here's a link to Windows File Management Functions.

SECOND EDIT:

If you want a cross-platform file system library, supporting among other things directory creation, you could check the boost filesystem library.

这篇关于C / C ++中的简单虚拟文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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