繁体   English   中英

将数据从文件加载到C中的数组

[英]Loading data from a file to an array in c

我想从以前保存的文件中加载数组。 我在下面运行了代码,但是由于某种原因,当我加载数组时,加载的数组长度与我保存的数组长度不同。 如何更改加载文件代码的长度,以使其适用于任何数组长度?

intarr_t* intarr_load_binary( const char* filename )
{
    unsigned int len = 0;
    FILE *f = fopen (filename, "rb");
    fscanf (f, "%d", len);
    intarr_t* newia = malloc (sizeof(intarr_t));
    assert (newia);
    newia->data = malloc (len*sizeof(int));
    assert(newia->data);
    newia->len = len;
    if (f != NULL)
    {
        while (!feof(f))
        {
            fscanf (f, "%d", newia->data);
        }
    }
    else
    {
        return NULL;
    }
    fclose (f);
    return newia;
}

我用于保存/加载的结构在这里:

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

我用来保存文件的代码在这里:

int intarr_save_binary( intarr_t* ia, const char* filename )
{
    unsigned int len = ia->len;
    FILE *f;
    f = fopen (filename, "wb");
    if (fwrite (ia->data, sizeof(int), len, f) == len)
    {
        return 0;
    }
    else
    {
        return 1;
    }
    fclose (f);
}
the code is writing no length (len) value as the first data item to the file
yet the code is reading a length (len) value 
as if it were the first data item in the file.

this code is full of errors and oversights:

int intarr_save_binary( intarr_t* ia, const char* filename )
{
    unsigned int len = ia->len;
    FILE *f;
    f = fopen (filename, "wb");
    if (fwrite (ia->data, sizeof(int), len, f) == len)
    {
        return 0;
    }
    else
    {
        return 1;
    }
    fclose (f);
}

suggest using code similar to this:

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 )
    { // then write of length successful

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

        else
        { // else, write of data failed
            returnValue = 3;
        }
    }
    else
    { // else, failed to write len value to file
        returnValue = 4;
    }

    fclose( f ); // cleanup (writes last buffer to file)
    return( returnValue );
} // end function: intarr_save_binary
this code needs some work.  
for instance because assert should not enabled in production code
and error conditions not being checked
and cleanup not being properly performed

if the program is ok to continue after an I/O error 
by always returning NULL
then you could change the following, 
to return NULL rather than exiting the program

intarr_t* intarr_load_binary( const char* filename )
{
    unsigned int len = 0;
    FILE *f = fopen (filename, "rb");
    fscanf (f, "%d", len);
    intarr_t* newia = malloc (sizeof(intarr_t));
    assert (newia);
    newia->data = malloc (len*sizeof(int));
    assert(newia->data);
    newia->len = len;
    if (f != NULL)
    {
        while (!feof(f))
        {
            fscanf (f, "%d", newia->data);
        }
    }
    else
    {
        return NULL;
    }
    fclose (f);
    return newia;
}

suggest:

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

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

    // implied else, fopen successful

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

    // implied else, malloc successful

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

    // implied else, fread for len successful

    newis->len = len;

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

    // implied else, malloc successful

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

    // implied else, fread successful

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM