# mco_class_save
RT

> Save an entire class to an external file.

### Prototype

        MCO_RET	mco_class_save( /*IN*/ uint2 class_code,
                      /*IN*/ void * stream_handle,
                      /*IN*/ mco_stream_write output_stream_writer,
                      /*IN*/ mco_db_h db);

### Arguments

class_code | Integer class code (from generated header file)
stream_handle | Handle to the output stream
output_stream_writer | Handler function called by the runtime to format output
db | The database handle

### Description

> This function is used to stream the entire contents of a database class to permanent storage. Used together with `mco_class_load()` an application can save and optionally load class data. Note that a transaction is opened implicitly for this operation – no transaction handle is required.

> The `mco_class_save()` / `mco_class_load()` combination is compatible with `mco_db_save()` /`mco_db_load()`. That means that the application can save the entire database image with `mco_db_save()` and later load (or reload) the content of a particular class from the full image. Or it's possible to save the content of only one class with `mco_class_save()` and then use this image to create a database with the `mco_db_load()`.

> See the control structure stream writer for further details.

### Return Codes

MCO_S_OK | The database was closed successfully
MCO_E_NOINSTANCE | The specified database is not opened
MCO_E_WRITE_STREAM | Error writing to output stream

### Example

        Application snippet:

        const char * dbname = "SimpleDb";

        /* Stream writer with prototype mco_stream_write */
        mco_size_sig_t file_writer( void *stream_handle /* FILE *  */, const void *from, mco_size_t nbytes )
        {
            FILE *f = (FILE *)stream_handle;
            mco_size_sig_t nbs;
            nbs = fwrite( from, 1, nbytes, f );
            return nbs;
        }

        int main(int argc, char* argv[])
        {
            mco_db_h db;
            MCO_RET rc;
            FILE * fbak;
            uint2 my_class_code = 1;
            ...

            if( (rc = mco_runtime_start()) != MCO_S_OK)
                exit(-1);

            rc = mco_db_open_dev( dbname, simpledb_get_dictionary(), &dev, 1, &db_params );
            if ( MCO_S_OK != rc )
            {
                rc = mco_db_connect(dbname, &db);
                ...

                /* Backup database */
                fbak = fopen( "myclass.bak", "wb" );
                if ( fbak == 0 )
                {
                    printf( "\n\t Can't open output file for streaming\n");
                }
                else
                {
                    rc = mco_class_save( my_class_code, (void *)fbak, file_writer, db );

                    fclose( fbak );
                    sample_rc_check( "\t Save class", rc );
                }
                ...
            }
        }

### Files

Header file:
mco.h
Source file:
mcosericmn.c
Library:
libmcoseri.a