Troubles with files in C












-2















I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.



#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "intarr.h"

/* LAB 6 TASK 1 */

/*
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 intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );

if( f == NULL )
{
return 1;
}

if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}

if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}

fclose( f );

return 0;
}

/*
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.
*/

intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}

FILE* f = fopen( "filename", "rb" );

if( f == NULL )
{
return NULL;
}

int len;

if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}

intarr_t* new_ia = intarr_create( len );

fread( new_ia->data, sizeof( int ), len, f );

fclose( f );

return new_ia;
}


Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)










share|improve this question

























  • What data are you saving? What is wrong with the data that is loaded?

    – Retired Ninja
    Nov 21 '18 at 0:59











  • Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?

    – paulsm4
    Nov 21 '18 at 1:02











  • We can't tell what type ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.

    – David Schwartz
    Nov 21 '18 at 1:15











  • Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the fread call.

    – paddy
    Nov 21 '18 at 1:15
















-2















I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.



#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "intarr.h"

/* LAB 6 TASK 1 */

/*
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 intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );

if( f == NULL )
{
return 1;
}

if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}

if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}

fclose( f );

return 0;
}

/*
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.
*/

intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}

FILE* f = fopen( "filename", "rb" );

if( f == NULL )
{
return NULL;
}

int len;

if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}

intarr_t* new_ia = intarr_create( len );

fread( new_ia->data, sizeof( int ), len, f );

fclose( f );

return new_ia;
}


Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)










share|improve this question

























  • What data are you saving? What is wrong with the data that is loaded?

    – Retired Ninja
    Nov 21 '18 at 0:59











  • Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?

    – paulsm4
    Nov 21 '18 at 1:02











  • We can't tell what type ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.

    – David Schwartz
    Nov 21 '18 at 1:15











  • Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the fread call.

    – paddy
    Nov 21 '18 at 1:15














-2












-2








-2








I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.



#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "intarr.h"

/* LAB 6 TASK 1 */

/*
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 intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );

if( f == NULL )
{
return 1;
}

if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}

if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}

fclose( f );

return 0;
}

/*
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.
*/

intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}

FILE* f = fopen( "filename", "rb" );

if( f == NULL )
{
return NULL;
}

int len;

if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}

intarr_t* new_ia = intarr_create( len );

fread( new_ia->data, sizeof( int ), len, f );

fclose( f );

return new_ia;
}


Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)










share|improve this question
















I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.



#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "intarr.h"

/* LAB 6 TASK 1 */

/*
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 intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );

if( f == NULL )
{
return 1;
}

if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}

if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}

fclose( f );

return 0;
}

/*
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.
*/

intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}

FILE* f = fopen( "filename", "rb" );

if( f == NULL )
{
return NULL;
}

int len;

if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}

intarr_t* new_ia = intarr_create( len );

fread( new_ia->data, sizeof( int ), len, f );

fclose( f );

return new_ia;
}


Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)







c file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 0:59







zaack7

















asked Nov 21 '18 at 0:53









zaack7zaack7

63




63













  • What data are you saving? What is wrong with the data that is loaded?

    – Retired Ninja
    Nov 21 '18 at 0:59











  • Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?

    – paulsm4
    Nov 21 '18 at 1:02











  • We can't tell what type ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.

    – David Schwartz
    Nov 21 '18 at 1:15











  • Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the fread call.

    – paddy
    Nov 21 '18 at 1:15



















  • What data are you saving? What is wrong with the data that is loaded?

    – Retired Ninja
    Nov 21 '18 at 0:59











  • Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?

    – paulsm4
    Nov 21 '18 at 1:02











  • We can't tell what type ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.

    – David Schwartz
    Nov 21 '18 at 1:15











  • Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the fread call.

    – paddy
    Nov 21 '18 at 1:15

















What data are you saving? What is wrong with the data that is loaded?

– Retired Ninja
Nov 21 '18 at 0:59





What data are you saving? What is wrong with the data that is loaded?

– Retired Ninja
Nov 21 '18 at 0:59













Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?

– paulsm4
Nov 21 '18 at 1:02





Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?

– paulsm4
Nov 21 '18 at 1:02













We can't tell what type ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.

– David Schwartz
Nov 21 '18 at 1:15





We can't tell what type ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.

– David Schwartz
Nov 21 '18 at 1:15













Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the fread call.

– paddy
Nov 21 '18 at 1:15





Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the fread call.

– paddy
Nov 21 '18 at 1:15












1 Answer
1






active

oldest

votes


















1














In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:



fwrite( &ia->data, sizeof( int ), ia->len, f )


The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:



fwrite( ia->data, sizeof( int ), ia->len, f )





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403821%2ftroubles-with-files-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:



    fwrite( &ia->data, sizeof( int ), ia->len, f )


    The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:



    fwrite( ia->data, sizeof( int ), ia->len, f )





    share|improve this answer




























      1














      In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:



      fwrite( &ia->data, sizeof( int ), ia->len, f )


      The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:



      fwrite( ia->data, sizeof( int ), ia->len, f )





      share|improve this answer


























        1












        1








        1







        In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:



        fwrite( &ia->data, sizeof( int ), ia->len, f )


        The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:



        fwrite( ia->data, sizeof( int ), ia->len, f )





        share|improve this answer













        In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:



        fwrite( &ia->data, sizeof( int ), ia->len, f )


        The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:



        fwrite( ia->data, sizeof( int ), ia->len, f )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 1:12









        paddypaddy

        43.1k53176




        43.1k53176
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403821%2ftroubles-with-files-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings