[ACCEPTED]-Simple Virtual Filesystem in C/C++-filesystems

Accepted answer
Score: 13

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

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

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


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


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

Score: 7

I don't think you can compile a module for 1 different OSes, the way you want.

// Make the distinction at compile time,

FILE VFS_File_Open( const unsigned char strFile, int flags )
#ifdef _WINDOWS
    //implement here the system calls required to open a file on a WIN OS
#ifdef _LINUX
    //implement here the system calls required to open a file on a Linux OS
Score: 6

You could try PhysicsFS library.


Score: 1

Actually implemented this, so from experience 11 here:

The first thing to do is use classes. There 10 is no fopen() equivalent. If there are flags, they're 9 going to be an enum. Filenames are wchar_t 8 nowadays.

The second thing to do is factor 7 out the OS-dependent parts of your file 6 class. They should be in separate methods. You 5 move these to a seperate file. For every 4 OS you have, there will be a different file 3 implementing the same methods. When you 2 link your app, you know the target architecture 1 and you can pick the correct versions.

Score: 0

Standard C has no such feature. Notice that 17 the notion of "concrete OS" is also a bit 16 vague: are Windows XP and Windows Vista 15 the same "concrete OS", or different ones? Are 14 CentOS and Ubuntu the same OS, or different 13 ones?

Apparently, you are only looking for 12 API differences, so in most cases, you can 11 probably ignore version differences and 10 distribution differences (although both 9 Windows and Linux grow new system calls 8 from time to time). In this case, it is 7 best to preprocessor conditional compilation 6 - since the code making Linux-specific calls 5 won't even compile on Windows.

Traditionally, the 4 system compilers have various macros predefined. Here 3 are a few such macros, on respective systems: _WIN32, __linux, __linux__, __AIX__, __hpux, ... If 2 you need to identify a specific system, you 1 should ask again on SO.

Score: 0

If you're looking for a way to abstract 12 yourself from the properties of the filesystem 11 you're using (path separators etc.), and 10 if you're happy with a C++-only solution, take 9 a look at Boost.Filesystem.

Paths can be specified in the 8 portable generic path format (basically POSIX format) and are automatically 7 converted to the native format:

path my_path( "some_dir/file.txt" );

Elements 6 can be concatenated onto the path using 5 the / operator, and the result can then directly 4 be used to open a file:

ifstream file1( my_path / "foo/bar" );

What's more, this 3 functionality is part of Technical Report 2 2, meaning that it will likely make its 1 way into the standard library.

More Related questions