By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

trouble, writing an array of structure to binary file.

P: n/a
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[i] = 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
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jens Mittag wrote:

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;
These declaration belong way up there, under FILE *... As it is
it should not even compile under any self-respecting C compiler.
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[i] = eintrag;
}
records = e;
e is local. At function exit its contents are lost and its
address becomes meaningless.
return 0;
However the file is still open. Naughty.

} 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


Try to publish complete, compilable, source which demonstrates the
problem. Also limit line length to 65 or so, and do not use //
comments or tabs in newsgroups.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #2

P: n/a
Am Sun, 06 Jun 2004 16:19:13 +0000 schrieb CBFalconer:
Try to publish complete, compilable, source which demonstrates the
problem. Also limit line length to 65 or so, and do not use //
comments or tabs in newsgroups.


Thanks. Will try all the stuff you mentioned.

Jens
Nov 14 '05 #3

P: n/a


Jens Mittag wrote:
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.
You have not shown code that details how you came to this
conclusion. Because of this, one cannot specifically point to
the cause of the unexpected behavior. However, in the code you
have provided, there is one glaring error in function
LoadDirectory involving the creation of the array.

*/

#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];
e is declared and exists only in function loadDirectory.

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[i] = eintrag;
}
records = e;
The global pointer, records, points to this function's local array. Once
the function concludes its execution, the local variable will cease
to exist and the value of records is useless. A quick solution is to
dynamically allocate using function malloc or realloc to create the
array. The maintenance involved in the dynamic memory manage can
become complex. To help reduce the complexity, I would suggest you
modify your datatype and write functions that you can use to
safely manipulate the datatype. Although I do not know the specific
purpose of your datatype, I have provided, below, an example that might
help you.

return 0;

} else return -1;
}

void saveDirectory () {
FILE *fdirectory = fopen (DIRECTORY_FILE, "wb");


.......snip.......
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define DIRECTORY_FILE "index.dat"

typedef struct
{
int local_depth;
int elements;
int address;
} table_element;

typedef struct
{
table_element *records;
unsigned global_depth;
} table_array;

/* Prototypes */
int loadDirectory(table_array *p);
int createArray(table_array *p, unsigned global_depth);
int saveDirectory (table_array *p);
void freeArray(table_array *p);
void printArray(table_array *p);

int main(void)
{
table_element test[4] = {{ 1,2,3},{2,3,4},{3,4,5},{6,7,8}};
table_array my = {NULL};
unsigned i,depth;

depth = 2;
if(!createArray(&my,depth)) exit(EXIT_FAILURE);
for(i = 0;i < (unsigned)pow(2,depth);i++)
my.records[i] = test[i];
printArray(&my);
if(saveDirectory(&my)) puts("\nSaving the array to file");
else
{
puts("\nFailure in saving the array to file");
exit(EXIT_FAILURE);
}
puts("\nFreeing the array\n");
freeArray(&my);
printArray(&my);
puts("\nloading the array from file\n");
if(loadDirectory(&my)) printArray(&my);
else puts("Failure in loading array from file");
freeArray(&my);
return 0;
}

int loadDirectory(table_array *p)
{
FILE *directoryFile = fopen (DIRECTORY_FILE, "rb");

if (directoryFile == NULL) return 0;
else
{
unsigned size,i;

freeArray(p);
if(1 != fread (&p->global_depth, sizeof (int), 1,
directoryFile))
{
freeArray(p);
return 0;
}
size = (unsigned)pow(2,p->global_depth);
p->records = malloc(size * (sizeof *p->records));
if(!p->records)
{
freeArray(p);
return 0;
}
for (i=0; i < size ; i++)
if(1 != fread(&p->records[i],sizeof(table_element),
1, directoryFile))
{
freeArray(p);
return 0;
}
}
fclose(directoryFile);
return 1;
}

int createArray(table_array *p, unsigned global_depth)
{
table_element *tmp;

if((tmp = realloc(p->records,
(size_t)pow(2,global_depth)*(sizeof *p->records))) == NULL)
return 0;
p->records = tmp;
p->global_depth = global_depth;
return 1;
}

int saveDirectory (table_array *p)
{
FILE *fdirectory = fopen (DIRECTORY_FILE, "wb");

if (fdirectory == NULL || p->global_depth == 0) return 0;
else
{
unsigned size;
if(1 != fwrite (&p->global_depth, sizeof (int),
1, fdirectory))
return 0;
size = (unsigned)pow(2,p->global_depth);
if(size != fwrite(p->records, sizeof(table_element),
size, fdirectory))
return 0;
fclose (fdirectory);
}
return 1;
}

void freeArray(table_array *p)
{
free(p->records);
p->records = NULL;
p->global_depth = 0;
return;
}

void printArray(table_array *p)
{
unsigned i;

if(p->global_depth == 0) puts("No Allocated Array");
else
{
for(i = 0;i < (unsigned)pow(2,p->global_depth);i++)
printf("Element #%u\n\tlocal_depth = %d\n\t"
"Elements = %d\n\tAddress = %d\n\n",
i+1,p->records[i].local_depth,
p->records[i].elements,p->records[i].address);
}
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4

P: n/a
On Sun, 06 Jun 2004 16:19:13 GMT, CBFalconer <cb********@yahoo.com>
wrote:
Jens Mittag wrote:

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);
More idiomatic, and probably more efficient: 1<<global_depth.
printf ("Size = %d\n", size);

table_element e[size];
table_element eintrag;
int i;


These declaration belong way up there, under FILE *... As it is
it should not even compile under any self-respecting C compiler.

They should under any C99; or gcc, which respects itself perhaps more
than it should. And if neither of those, then moving them is still
wrong as size is not a constant expression.
for (i=0; i < (int) pow(2,global_depth); i++) {
Since you've already computed 'size', (re)use it.
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[i] = eintrag;
}
Or, if you don't need the outputs, or can do them separately/later,
replace the whole loop by one
fread (e /* or records */, sizeof *e, size, directoryFile)
preferably with also
if( fread_as_above != size ) /* handle error appropriately */
records = e;


e is local. At function exit its contents are lost and its
address becomes meaningless.

Yep, that's the real killer.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.