0
#include <stdio.h>
#include <stdlib.h>

struct SomeStruct{
    int id;
    int* dynamicArray;
};

int main(){
    struct SomeStruct test;
    test.id = 5;
    // allocate array
    test.dynamicArray = malloc(sizeof(int)*100);
    // save struct to a file
    FILE* handle = fopen("data", "wb");
    fwrite(&test, sizeof(test), 1, handle);
    fclose(handle);
    return 0;
}

After running this code the size of data file is 16 bytes.obviously an integer pointer has been written to the file instead of whole dynamicArray elements. How can I write such structure correctly in a file?

6
  • Write the data pointed to by dynamicArray, and not the struct, which contains the pointer to the data of the array, and not the array itself. Commented Mar 20, 2018 at 18:30
  • You should create some simple format to store data. At least the dynamic array content should be stored in the file( more complicated if itself contains pointers), and a way to associate SomeStruct with that data in the format. Commented Mar 20, 2018 at 18:32
  • 1
    There is no merit in writing the pointer to file - you should write the data it points to. It would be advantageous for the struct to have a member size_t size so you know how much memory to allocate, and elements to read, when you reload the file later. Commented Mar 20, 2018 at 18:32
  • You keep track of how many entries are active in the array (or the max array size if you wish) and write that out after you write the id. Might be nice to write out the number of used elements into the structure as well for ease of reading later... Commented Mar 20, 2018 at 18:32
  • 1
    Also consider how you would read the data back from a file, that should also hint to how you want to write the data. Commented Mar 20, 2018 at 18:32

1 Answer 1

3
  1. Write the id.
  2. Then write the array.

fwrite(&test.id, sizeof(test.id), 1, handle);
fwrite(test.dynamicArray , sizeof(test.dynamicArray[0]), 100, handle);

I want to point out that use of the hard coded number 100 is problematic.

  1. You have to make sure that the read function also assumes that.
  2. If you decide to change the number of elements allocated, you'll have to remember to go back to both places (the write and the read) to update them.

It will be better to store the size in the struct itself.

struct SomeStruct{
    int id;
    size_t size;
    int* dynamicArray;
};

and

test.id = 5;
test.size = 100;
// allocate array
test.dynamicArray = malloc(sizeof(int)*test.size);

Then you can update the write logic to:

fwrite(&test.id, sizeof(test.id), 1, handle);
fwrite(&test.size, sizeof(test.size), 1, handle);
fwrite(test.dynamicArray , sizeof(test.dynamicArray[0]), test.size, handle);

When you read the data back in, you will have the size information available to you and you can allocate the right amount memory for the dynamicArray member.

Sign up to request clarification or add additional context in comments.

1 Comment

you are right. it should work. but as @Nit mentioned it is hard to read data back from file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.