Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Save the entire array ia into a file called \'filename\' in a binary file format

ID: 3807829 • Letter: S

Question

Save the entire array ia into a file called 'filename' in a binary file format that can be loaded by intarr load binary(). Returns zero on success, or a non-zero error code on failure. Arrays of length 0 should produce an output file containing an empty array int i save binary( intarr t ia, const char* filename Load a new array from the file called filename that was previously saved using intarr save binary Returns a pointer to a newly-allocated intarr t on success or NULL on failure intarrt intarr load binary const ch ar filename Requirements 1. Add and commit a single C source file called "t1.c" containing implementations of these two functions. 2. The file must include the intarr h" header file. 3. The code may call any other functions declared in "intarr.h". Your code will be linked against our reference implementation for testing, so make sure the submitted file does not contain functions with the same names 4. Use your own implementation of the intam t functions for your local testing. Note that you do not need to have completed all of Lab 5 to do this, but seek help right away if you have not completed intarr create() at least. 5. Performance hint calls to write0 are relatively expensive. Try to use as few as you can

Explanation / Answer

#include "intarr.h"
#include <stdlib.h>
#include <stdio.h>

typedef struct {
int* data;
unsigned int len;
} intarr_t;


/*Load binary*/
intarr_t* intarr_load_binary( const char* filename )
{
    unsigned int len = 0;
    FILE *f = NULL;
    intarr_t* newia = NULL;

    if( NULL == (fopen (filename, "rb")) )
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    if( NULL == (newia = malloc (sizeof(intarr_t)))){
        perror( "malloc failed" );
        fclose(f);
        exit( EXIT_FAILURE );
    }

    if( (fread (&len, sizeof(int), 1, f) != 1 ) )
    {
        perror( "fread failed" );
        fclose(f);
        free( newia );
        exit( EXIT_FAILURE );
    }

    // implied else, fread for len successful

    newia->len = len;

    if( NULL == (newia->data = malloc (len*sizeof(int)) ) )
    {
        perror( "malloc failed" );
        fclose(f);
        free( newia );
        exit( EXIT_FAILURE );
    }

     if( fread( newia->data, sizeof(int), len, f ) != len )
    {
        perror( "fread failed" );
        fclose(f);
        free(newia->data);
        free(newia);
        exit( EXIT_FAILURE );
    }

    fclose (f);
    return newia;
} // end function: intarr_load_binary

/*Save the entire array into a new filename in a binary filename*/

int intarr_save_binary( intarr_t* ia, const char* filename )
{
    int returnValue = 0;
    unsigned int len = ia->len;
    FILE *f;

    if( NULL == (f = fopen (filename, "wb") ))
    {
        perror( "fopen failed" );
        returnValue = 1;
    }

    else if ( fwrite ( &len, sizeof(int), 1, f) == 1 )
    {

if (fwrite (ia->data, sizeof(int), len, f) == len)
        {
            returnValue = 0;
        }

else
        {
            returnValue = 3;
        }
    }
    else
    {
        returnValue = 4;
    }

    fclose( f );
    return( returnValue );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote