J
Jens Mittag
Hi!
In my code, I have an array of a structure, which I want to save to a
binary file. When the array is just created, everything works fine, but
when I change contents of the array, saving results in a file, which
doesn't hold the information of the changed array anymore. I dont know why.
Here is the code:
/*
1. loadDirectory() loads records from a binary file into an Array (of table_elements)
2. The address of the first element of the array is stored in 'records'.
3. saveDirectory() should save the array to a file again.
My problem is now: when I change some array elements and then save the
array to a file, and then reload the file via loadDirectory, the content of the
array-elements has changed. I dont know why.
*/
#define DIRECTORY_FILE "index.directory"
typedef struct {
int local_depth;
int elements;
int address;
} table_element;
table_element *records;
int global_depth;
int loadDirectory() {
FILE *directoryFile = fopen (DIRECTORY_FILE, "rb");
if (directoryFile != NULL) {
fread ( &global_depth, sizeof (int), 1, directoryFile);
printf ("Global Depth = %d\n", global_depth);
int size = (int) pow(2,global_depth);
printf ("Size = %d\n", size);
table_element e[size];
table_element eintrag;
int i;
for (i=0; i < (int) pow(2,global_depth); i++) {
fread ( &eintrag, sizeof (table_element), 1, directoryFile);
printf ("I've read the following: Local-Depth = %d, Elements = %d, Address = %d\n", eintrag.local_depth, eintrag.elements, eintrag.address);
e = eintrag;
}
records = e;
return 0;
} else return -1;
}
void saveDirectory () {
FILE *fdirectory = fopen (DIRECTORY_FILE, "wb");
if (fdirectory != NULL) {
fwrite (&global_depth, sizeof (int), 1, fdirectory);
fwrite (records, sizeof (table_element), (int) pow (2,global_depth), fdirectory);
fclose (fdirectory);
}
}
Thank you in advance. If the output is unreadable, have a look at
http://nopaste.php-q.net/63414
Greets Jens
In my code, I have an array of a structure, which I want to save to a
binary file. When the array is just created, everything works fine, but
when I change contents of the array, saving results in a file, which
doesn't hold the information of the changed array anymore. I dont know why.
Here is the code:
/*
1. loadDirectory() loads records from a binary file into an Array (of table_elements)
2. The address of the first element of the array is stored in 'records'.
3. saveDirectory() should save the array to a file again.
My problem is now: when I change some array elements and then save the
array to a file, and then reload the file via loadDirectory, the content of the
array-elements has changed. I dont know why.
*/
#define DIRECTORY_FILE "index.directory"
typedef struct {
int local_depth;
int elements;
int address;
} table_element;
table_element *records;
int global_depth;
int loadDirectory() {
FILE *directoryFile = fopen (DIRECTORY_FILE, "rb");
if (directoryFile != NULL) {
fread ( &global_depth, sizeof (int), 1, directoryFile);
printf ("Global Depth = %d\n", global_depth);
int size = (int) pow(2,global_depth);
printf ("Size = %d\n", size);
table_element e[size];
table_element eintrag;
int i;
for (i=0; i < (int) pow(2,global_depth); i++) {
fread ( &eintrag, sizeof (table_element), 1, directoryFile);
printf ("I've read the following: Local-Depth = %d, Elements = %d, Address = %d\n", eintrag.local_depth, eintrag.elements, eintrag.address);
e = eintrag;
}
records = e;
return 0;
} else return -1;
}
void saveDirectory () {
FILE *fdirectory = fopen (DIRECTORY_FILE, "wb");
if (fdirectory != NULL) {
fwrite (&global_depth, sizeof (int), 1, fdirectory);
fwrite (records, sizeof (table_element), (int) pow (2,global_depth), fdirectory);
fclose (fdirectory);
}
}
Thank you in advance. If the output is unreadable, have a look at
http://nopaste.php-q.net/63414
Greets Jens