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;
}
fread(&data[0], sizeof(char), len, f);
it barfs on that line. Does that give you any clues?gcc -Wall -Werror -o program program.c
where you replaceprogram
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.