0

I hope the title is correct, i cant describe it better, sorry. I have a struct with pointers thats propagated during the "session" of a program. Of course, this structure is thrown everytime so i want to save the current state to a binary file. Because entry:data is a pointer to an array of unknown length, i tried to append the length to each entry. So far everything is working fine, but when i want to delete or append an antry i run into problems. I want to create an array of (struct entry) with the length thats in the file header but as soon as i create the struct the function doesnt work anymore.

Here is a minimal working example

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

struct header {
    int id;
    int len;
};

struct entry {
    int id;       
    int len; // length of data        
    char *data;   
};

void createNewStorage() {

    FILE *f = fopen("test.bin", "wb");   

    // create a header with id=1 and len=2 
    struct header hdr = {1, 2};
    fwrite(&hdr, sizeof(struct header), 1, f);

    char *data;

    data = "This is some data of unknown length, i hope it works";
    int id = 1;
    int len = strlen(data);    
    struct entry e = {id, len, data};
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), strlen(e.data), f);

    id = 2; 
    data = "\x01\x12\x23"; 
    len = strlen(data); 
    e.id = id;
    e.data = data;
    e.len = len;
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), e.len, f);

    fclose(f);

}

void appendNewEntry() {

    FILE *f = fopen("test.bin", "rb");       

    // get the header
    struct header hdr;
    fread(&hdr.id, sizeof(int), 1, f);
    fread(&hdr.len, sizeof(int), 1, f);
    printf("hdr: id='%d', len='%d'\n", hdr.id, hdr.len);

    // get the entries
    int id, len = 0; char *data; 
    int i = 0;
    while(i < hdr.len) {    
        fread(&id, sizeof(int), 1, f);
        fread(&len, sizeof(int), 1, f);
        fread(&data, sizeof(char), len, f);
        printf("entry: id='%d', len='%d', data='%s'\n", id, len, &data);
        i++;        
    }    
    printf("Done..\n");
    fclose(f);

}

int main(void){
    createNewStorage();
    appendNewEntry();
    return 0;
}

Thank you in advance.

Edit: Finally i got it working, here is the final code.

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

struct header {
    int id;
    int len;
};

struct entry {
    int id;       
    int len; // length of data        
    char *data;   
};

void create() {
    FILE *f = fopen("test.bin", "wb");   
    // create a header with id=1 and len=2 
    struct header hdr = {1, 2};
    fwrite(&hdr, sizeof(struct header), 1, f);
    char *data;
    data = "This is some data of unknown length, i hope it works";
    int id = 1;
    int len = strlen(data);    
    struct entry e = {id, len, data};
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), strlen(e.data), f);
    id = 2; 
    data = "\x01\x12\x23"; 
    len = strlen(data); 
    e.id = id;
    e.data = data;
    e.len = len;
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), e.len, f);
    fclose(f);
}

void modify() {
    FILE *f = fopen("test.bin", "rb");       
    // get the header
    struct header hdr;
    fread(&hdr.id, sizeof(int), 1, f);
    fread(&hdr.len, sizeof(int), 1, f);
    printf("Reading storage hdr: id='%d', len='%d' ", hdr.id, hdr.len);
    // create a new list to store all existing entries
    struct entry list[hdr.len];    
    // get the entries
    int id, len = 0; char *data; 
    int i = 0;
    while(i < hdr.len) {    
        fread(&id, sizeof(int), 1, f);
        fread(&len, sizeof(int), 1, f);
        data = malloc((len + 1) * sizeof(char));
        fread(data, sizeof(char), len, f);
        data[len] = '\0';
        //printf("entry: id='%d', len='%d', data='%s'\n", id, len, &data[0]);
        struct entry e = {id, len, data};
        list[i] = e;
        i++;        
    }    
    printf("Done..\n");
    printf("The list has %d entries\n", i);
    int il = 0;
    while(il < i) {
        printf("entry %d: len='%d', data='%s'\n", il, list[il].len, list[il].data);
        il++;
    }    
    fclose(f);
}

void append(char *newdata) {
    printf("Appending new data..\n");
    FILE *f = fopen("test.bin", "rb");       
    // get the header
    struct header hdr;
    fread(&hdr.id, sizeof(int), 1, f);
    fread(&hdr.len, sizeof(int), 1, f);
    printf("Reading storage hdr: id='%d', len='%d' ", hdr.id, hdr.len);
    // create a new list to store all existing entries
    struct entry list[hdr.len];    
    // get the entries
    int id, len = 0; char *data; 
    int i = 0;
    while(i < hdr.len) {    
        fread(&id, sizeof(int), 1, f);
        fread(&len, sizeof(int), 1, f);
        data = malloc((len + 1) * sizeof(char));
        fread(data, sizeof(char), len, f);
        data[len] = '\0';
        //printf("entry: id='%d', len='%d', data='%s'\n", id, len, &data[0]);
        struct entry e = {id, len, data};
        list[i] = e;
        i++;        
    }    
    printf("Done..\n");
    /*
    printf("The list has %d entries\n", i);
    int il = 0;
    while(il < i) {
        printf("entry %d: len='%d', data='%s'\n", il, list[il].len, list[il].data);
        il++;
    }
    */
    fclose(f);    
    f = fopen("test.bin", "wb");    
    struct header outhdr = {hdr.id, hdr.len+1};
    fwrite(&outhdr, sizeof(struct header), 1, f);    
    int il = 0;
    while(il < i) {
        printf("writing entry %d\n", il, list[il].len, list[il].data);
        fwrite(&list[il].id, sizeof(int), 1, f);
        fwrite(&list[il].len, sizeof(int), 1, f);
        fwrite(&list[il].data[0], sizeof(char), list[il].len, f);
        il++;
    }
    il++;
    struct entry e;
    int nid = il;  
    int nlen = strlen(newdata); 
    e.id = nid;
    e.data = newdata;
    e.len = nlen;
    printf("writing new entry\n");
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), e.len, f);        
    fclose(f);    
}

void erase(int entryId) {
    printf("Appending new data..\n");
    FILE *f = fopen("test.bin", "rb");       
    // get the header
    struct header hdr;
    fread(&hdr.id, sizeof(int), 1, f);
    fread(&hdr.len, sizeof(int), 1, f);
    printf("Reading storage hdr: id='%d', len='%d' ", hdr.id, hdr.len);
    // create a new list to store all existing entries
    struct entry list[hdr.len];    
    // get the entries
    int id, len = 0; char *data; 
    int i = 0;
    while(i < hdr.len) {    
        fread(&id, sizeof(int), 1, f);
        fread(&len, sizeof(int), 1, f);
        data = malloc((len + 1) * sizeof(char));
        fread(data, sizeof(char), len, f);
        data[len] = '\0';
        //printf("entry: id='%d', len='%d', data='%s'\n", id, len, &data[0]);
        struct entry e = {id, len, data};
        list[i] = e;
        i++;        
    }    
    printf("Done..\n");
    /*
    printf("The list has %d entries\n", i);
    int il = 0;
    while(il < i) {
        printf("entry %d: len='%d', data='%s'\n", il, list[il].len, list[il].data);
        il++;
    }
    */
    fclose(f);
    f = fopen("test.bin", "wb");    
    struct header outhdr = {hdr.id, hdr.len-1};
    fwrite(&outhdr, sizeof(struct header), 1, f);    
    int il = 0;
    while(il < i) {
        if(list[il].id == entryId) {
            // this is the one to delete
            //printf("THIS IS IT -> %d\n", il);
        } else {
            printf("writing entry %d\n", il, list[il].len, list[il].data);
            fwrite(&list[il].id, sizeof(int), 1, f);
            fwrite(&list[il].len, sizeof(int), 1, f);
            fwrite(&list[il].data[0], sizeof(char), list[il].len, f);
        }
        il++;
    }       
}

int main(void){

    create();

    getch();

    char *input;
    input = "Mein Hund ist der aller aller Beste";
    append(input);

    getch();

    erase(1);

    return 0;
}
14
  • 1
    Can you please describe in more detail what "doesnt work" means for your program? What is the expected result and actual result?
    – kaylum
    Dec 19, 2019 at 22:20
  • 'doesnt work' menas it just stops. have you tried to reproduce it? does it work for you? on my computer the curser icon pops up the loading cursor and then the console crashes. There is nothing more i can say, really.
    – phpnoob
    Dec 19, 2019 at 22:24
  • You haven't provided a minimal verifiable example which makes it hard to try. Please do so.
    – kaylum
    Dec 19, 2019 at 22:26
  • 1
    Run it in a debugger and it can point you immediately to the problem: fread(&data[0], sizeof(char), len, f); it barfs on that line. Does that give you any clues?
    – kaylum
    Dec 19, 2019 at 22:32
  • 1
    Before going any further, compile your code with gcc -Wall -Werror -o program program.c where you replace program with your actual file name. This will display most warnings and treat warnings as errors. That is to say, you won't be able to run the code until you fix the code causing the warnings.
    – user3657941
    Dec 19, 2019 at 22:41

1 Answer 1

0

Try this:

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

struct header {
    int id;
    int len;
};

struct entry {
    int id;
    int len; // length of data
    char *data;
};

void createNewStorage() {

    FILE *f = fopen("test.bin", "wb");

    // create a header with id=1 and len=2
    struct header hdr = {1, 2};
    fwrite(&hdr, sizeof(struct header), 1, f);

    char *data;

    data = "This is some data of unknown length, i hope it works";
    int id = 1;
    int len = strlen(data);
    struct entry e = {id, len, data};
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), strlen(e.data), f);

    id = 2;
    data = "\x01\x12\x23";
    len = strlen(data);
    e.id = id;
    e.data = data;
    e.len = len;
    fwrite(&e.id, sizeof(int), 1, f);
    fwrite(&e.len, sizeof(int), 1, f);
    fwrite(e.data, sizeof(char), e.len, f);

    fclose(f);

}

void appendNewEntry() {

    FILE *f = fopen("test.bin", "rb");

    // get the header
    struct header hdr;
    fread(&hdr.id, sizeof(int), 1, f);
    fread(&hdr.len, sizeof(int), 1, f);
    printf("hdr: id='%d', len='%d'\n", hdr.id, hdr.len);

    // get the entries
    int id, len = 0; char *data;
    int i = 0;
    while(i < hdr.len) {
        fread(&id, sizeof(int), 1, f);
        fread(&len, sizeof(int), 1, f);
        data = malloc((len + 1) * sizeof(char));
        fread(data, sizeof(char), len, f);
        data[len] = '\0';
        printf("entry: id='%d', len='%d', data='%s'\n", id, len, data);
        i++;
    }
    printf("Done..\n");
    fclose(f);

}

int main(void){
    createNewStorage();
    appendNewEntry();
    return 0;
}

Output

hdr: id='1', len='2'
entry: id='1', len='52', data='This is some data of unknown length, i hope it works'
entry: id='2', len='3', data='#'
Done..

Note

I had to add #inlude <stdlib.h> to use malloc. The original code was using &data in fread and printf unnecessarily. Also, it does not look like the data is actually read into a list of entry, which seems odd.

Warning

This code allocates memory and it is the users responsibility to free the memory before the program ends.

3
  • Before going any further, compile your code with gcc -Wall -Werror. Any errors? Try to replace "&data" with "&data[0]".
    – phpnoob
    Dec 19, 2019 at 22:53
  • @phpnoob I have a version of your code that runs and prints out the data. Have a look.
    – user3657941
    Dec 19, 2019 at 23:38
  • Nice, thank you very much! Sorry for my inpatience. Its getting harder and harder to ask on SO. I have edited my question, see my final code including append and delete functions. ps: i hope you know my 1st comment was a bad joke :P
    – phpnoob
    Dec 19, 2019 at 23:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.