You copied the Doc URL to your clipboard.

2.4. Creating a filesystem interface

You can use the FileSystem interface to create streams for reading and writing files in the native or a virtual file system. Use the System::createFileSystem() function to retrieve a FileSystem object. As with other System objects, you can use Managed<> to eliminate the requirement to manually release the created object.

The following steps describe how to create a FileSystem interface:

  1. Open the main.cpp file from the 03 - Using the FileSystem Interface project.

    Example 2.11 shows how to create a system and filesystem interface:

    Example 2.11. Create a System object

    	
    #include <mde/mde.h>
    #include <cstdio>
    
    int main()
    {
        try
        {
           MDE::Managed<MDE::System> system = MDE::create_system_backend();
           Managed<FileSystem> filesystem = system->createFileSystem(".");
    ...
    

    The parameter passed to createFileSystem() specifies the root directory.

  2. Example 2.12 shows how to use the filesystem to attempt to read a file. For this example, the target file does not exist, and the exception is trapped.

    Example 2.12. Create an InputStream object

    	
    ...
            printf("Trying to load a non-existent file:\n");
    
            try
            {
                Managed<InputStream> nonexisting = filesystem->createInputStream("data/non-existing-file.txt");
            }
            catch(IOException& ioe)
            {
                printf("An expected exception was thrown:\n%s\n", ioe.getMessage().getCharString() );
            }
    
    ...    
    

  3. Example 2.13 shows how to create two streams and read the contents of a file:

    Example 2.13. Reading from a file

    	
    ...
               Managed<OutputStream> output = filesystem->createOutputStream("data/write.txt");
            Managed<InputStream> input = filesystem->createInputStream("data/read.txt");
        
            int length = input->getLength();
            // Allocate the size of the file, plus one extra byte for a null-terminator
            char* inputBuffer = new char[length+1];                
            input->read(inputBuffer, sizeof(char), length);
            inputBuffer[length] = '\0';                            // Add null-termination character
        }
    ...   
    

    To read or write to the stream, you must define:

    • a buffer to read from or write to

    • the size of each element

    • how many elements to read or write.

    The Filesystem methods are:

    • read() to read content from a file.

    • write() to write content to a file.

    • getLength() to get the length of the file

    • getPosition() to get the current position

    • setPosition() to manually set the position.

  4. Example 2.14 shows how to write the buffer to a file:

    Example 2.14. Writing to a file

    	
    ...
            output->write(inputBuffer, sizeof(char), length);
            output->flush();
    ...
    

  5. Example 2.15 shows deleting unused objects and the final catch block:

    Example 2.15. Deleting the buffer

    	
    ...
            delete [] inputBuffer;
        }
        catch(Exception& e)
        {
            printf("An unexpected exception was thrown:\n%s\n", e.getMessage().getCharString() );
        }
    }
    

    The system and filesystem objects do not require manual deletion because the Managed<> template tracks usage and automatically destroys the objects when the application exits.