1

I am trying to save a struct (listed)

typedef struct tupleStruct{

    int element[eMax];
    char * id;
    int eCount;

}tuple_t;

typedef struct {
    tuple_t * array;
    int used;
    int size;
} DynamicArray;

As part of an assignment I was instructed to save tuples that are stored in a dynamic array in a file. Unfortunately since strings don't exist in c (at least not like they do in other languages). Whenever I try to save an element of the dynamic array in a file, the string is not stored or loaded properly as it's seen as a pointer. I've even tried by initializing it like so in the struct:

char id[256];

Is there any way possible to save the struct and the string in a single file? (Given that I need to store multiple of these)

Edit: Saving and loading code

Loading

DynamicArray loadAllTuples(){

    FILE *filePointer;

    DynamicArray tempArray;

    if((filePointer=fopen("SavedTuples.bin","rb"))==NULL)
    {
        fputs("Something went wrong while loading!\nA blank Array will be loaded instead\n", stderr);
        setbuf(stdout, 0);

        //In case of error, blank array is initalised and loaded
        fclose(filePointer);

        intialiseDynamicArray(&tempArray);

        return tempArray;
    }



    fread(&tempArray, sizeof(DynamicArray),1,filePointer);

    //Freeing filePointer memory
    free(filePointer);

    return tempArray;

}

saving

void saveAllTuples(DynamicArray ToSave){

    trimArray(&ToSave,0); //Removing extra space from array

    FILE * filePointer;

    if((filePointer=fopen("SavedTuples.bin","wb"))==NULL)
    {
        fputs("Something went wrong while saving!\n", stderr);
        setbuf(stdout, 0);
        return;
    }

    fwrite(&ToSave, sizeof(ToSave), 1,filePointer);
    fclose(filePointer);

}

called by

saveAllTuples(dynaArray);

and

dynaArray=loadAllTuples();
10
  • The easiest way if you can give the string a fixed maximum size like 256 in your example. Commented Dec 18, 2018 at 15:56
  • @Lundin I tried that, however even in that scenario it still gets treated as a pointer. Commented Dec 18, 2018 at 15:58
  • strings do exist in c .. not entirely sure what you're asking. You can use fprintf to write a string to a file. It works just like printf except the first argument is a FILE* Commented Dec 18, 2018 at 15:58
  • Show us the code where you're writing the output and we can see where you're going wrong. Commented Dec 18, 2018 at 16:01
  • 1
    here's an example of one way to write a string (your char *) to file: stackoverflow.com/questions/4182876/… . After that, you still need to write the element array and eCount. Should you write the size of element too so that's recoverable, or is that fixed/able to be deduced from the data? You'll also need to write used and size from the DynamicArray. You'll need to write it all in a specific way such that the reader knows the format and can recover all the data exactly. Commented Dec 18, 2018 at 16:32

2 Answers 2

2

Instead of writing the whole struct in one go, write out the various parts of it utilising your knowledge of what they contain. So for example, if eCount is the amount of values in element you could write this

fwrite(&ToSave.eCount,sizeof(int), 1, filepointer);
fwrite(ToSave.element,sizeof(int), ToSave.eCount, filepointer);

and then to store the string component

size_t length=strlen(ToSave.id);
fwrite(&length,sizeof(int), 1, filepointer);
fwrite(ToSave.id,sizeof(char), length, filepointer);

Note: sizeof(char) is typically always 1, so you could assume that and put 1 rather than sizeof(char) but I find it makes the code look more uniform to leave it in.

And then you reverse the process when you do the reading

fwrite(&ToLoad.eCount,sizeof(int), 1, filepointer);
fwrite(ToLoad.element,sizeof(int), ToLoad.eCount, filepointer);
// etc...
Sign up to request clarification or add additional context in comments.

Comments

0

To whom it may help in the future: So after asking around a bit, some people got it to work by treating the identifier as an array instead of a pointer.

char id[256];

I said this didn't work before, but it was probably due to another mistake which I didn't spot. Saving each tuple will keep the string identifier intact as long as an array is used.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.