Confused

K

Kevin C.

Can someone explain why the file output produces all zeros? It seems to work
fine in memory (e.g. passing char pointers to printf) but when I output the
file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.)
and the output file is the correct size, just all zeros.


cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset);
cd_entry *dest = (cd_entry*)(cur + cur->cd_offset);
for(int i = 0; i < tpd->num_columns; i++)
{
*dest = *src;
dest += sizeof(cd_entry);
src += sizeof(cd_entry);
}

newlist->num_tables++;
free(g_tpd_list); // deallocate old memory
g_tpd_list = newlist;


//output to file
FILE *fhandle = fopen("dbfile.bin", "wbc");

if(fhandle == NULL)
return FILE_OPEN_ERROR;

fwrite(g_tpd_list, g_tpd_list->list_size, 1, fhandle);
fflush(fhandle);
fclose(fhandle);
 
J

Jens.Toerring

In comp.lang.c Kevin C. said:
Can someone explain why the file output produces all zeros? It seems to work
fine in memory (e.g. passing char pointers to printf) but when I output the
file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.)
and the output file is the correct size, just all zeros.
cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset);
cd_entry *dest = (cd_entry*)(cur + cur->cd_offset);
for(int i = 0; i < tpd->num_columns; i++)
{
*dest = *src;
dest += sizeof(cd_entry);
src += sizeof(cd_entry);
}

This looks as if you're trying to copy an array of structures, where
'src' points to the first structure of the source array and 'dest'
to the start of the destination array. If this is the case and
sizeof(cd_entry) isn't 1 by accident then this looks rather wrong,
you would need

for ( int i = 0; i < tpd->num_columns; src++, dest++, i++ )
*dest = *src;

On the other hand, using e.g. memcpy() might be faster then using a
loop in that case...
newlist->num_tables++;
free(g_tpd_list); // deallocate old memory
g_tpd_list = newlist;

//output to file
FILE *fhandle = fopen("dbfile.bin", "wbc");
if(fhandle == NULL)
return FILE_OPEN_ERROR;
fwrite(g_tpd_list, g_tpd_list->list_size, 1, fhandle);
fflush(fhandle);
fclose(fhandle);

I am also confused. But I am confused by your description of the
problem. I don't see a single char pointer which you could use
with printf() in that snippet of yours - all there is is some
'cd_entry' pointer, which you don't care to tell us what it is.
You're operating with a variable 'newlist' that's not defined
anywhere and has no recognizable type (but it is to be assumed
that it's a pointer to some structure, but that's pure guessword).
The first 8 lines don't seem to have any relevance to what you're
doing afterwards.

So you better try again and post the relevant parts of your program,
including typedefs etc. instead something that looks like the
result of a more or less random cut-and-paste operation.

Regards, Jens
 
K

kyle york

Greetings,
Can someone explain why the file output produces all zeros? It seems to work
fine in memory (e.g. passing char pointers to printf) but when I output the
file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.)
and the output file is the correct size, just all zeros.


cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset);
cd_entry *dest = (cd_entry*)(cur + cur->cd_offset);
for(int i = 0; i < tpd->num_columns; i++)
{
*dest = *src;
dest += sizeof(cd_entry);
src += sizeof(cd_entry);

Chnage the last two lines to:

dest ++
src ++

which will increment one element. Currently, you're incrementing
sizeof(cd_entry) *elements* (not chars) at a time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top