Problem with struct/array inside a struct using fread/fwrite
Author |
Message |
KSS Vijaya Bhask #1 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Hello All, For my project work, I am using a structure some thing like this struct Date { int day; int month; int year; Quote: };
struct LoanDetail { int loan_id; struct Date payment_dates[20]; double due_amounts[20]; double paid_amounts[20]; Quote: };
I am trying to store/read/update by storing these structures in a file on the seconday storage. To achieve, I am using fread/fwrite and also the file is created/read/updated all in 'binary' mode. But, I don't know, the limitations of fread/fwrite, I am having problems in successfully adding records, reading the records, so updation is also failing. From the above two structures, like 'Date', which doesn't have any nested member, it is not creating any problem. But, with a structure like 'LoanDetail', which has an array of structures(Date), array of doubles, etc. Can any one tell me the reason for fread/fwrite to fail while writing/reading structures having arrays as members. (I have also kept the size of the arrays as fixed, also tried with initializing all the unused members to some null values). But, still having problem. Looking forward for a reply. Thanks, Vijay
|
Tue, 23 Aug 2005 01:19:31 GMT |
|
|
Morris Dove #2 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Quote:
> For my project work, I am using a structure some thing like this > struct Date { > int day; > int month; > int year; > }; > struct LoanDetail { > int loan_id; > struct Date payment_dates[20]; > double due_amounts[20]; > double paid_amounts[20]; > }; > Can any one tell me the reason for fread/fwrite to fail while > writing/reading structures having arrays as members. (I have also > kept the size of the arrays as fixed, also tried with initializing all > the unused members to some null values). But, still having problem.
Vijay... Perhaps, but it's /very/ difficult without seeing the code manifesting the problem. Please post a minimal complete program that responders can cut, paste, and compile. -- Morris Dovey West Des Moines, Iowa USA C links at http://www.iedu.com/c
|
Tue, 23 Aug 2005 01:26:01 GMT |
|
|
KSS Vijaya Bhask #3 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Hello All, For my project work, I am using a structure some thing like this struct Date { int day; int month; int year; Quote: };
struct LoanDetail { int loan_id; struct Date payment_dates[20]; double due_amounts[20]; double paid_amounts[20]; Quote: };
I am trying to store/read/update by storing these structures in a file on the seconday storage. To achieve, I am using fread/fwrite and also the file is created/read/updated all in 'binary' mode. But, I don't know, the limitations of fread/fwrite, I am having problems in successfully adding records, reading the records, so updation is also failing. From the above two structures, like 'Date', which doesn't have any nested member, it is not creating any problem. But, with a structure like 'LoanDetail', which has an array of structures(Date), array of doubles, etc. Can any one tell me the reason for fread/fwrite to fail while writing/reading structures having arrays as members. (I have also kept the size of the arrays as fixed, also tried with initializing all the unused members to some null values). But, still having problem. Looking forward for a reply. Thanks, Vijay
|
Tue, 23 Aug 2005 01:39:49 GMT |
|
|
Morris Dove #4 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Quote:
> Hello All, > For my project work, I am using a structure some thing like this > struct Date { > int day; > int month; > int year; > }; > struct LoanDetail { > int loan_id; > struct Date payment_dates[20]; > double due_amounts[20]; > double paid_amounts[20]; > }; > I am trying to store/read/update by storing these structures in a file > on the seconday storage. To achieve, I am using fread/fwrite and also > the file is created/read/updated all in 'binary' mode. > But, I don't know, the limitations of fread/fwrite, I am having > problems in successfully adding records, reading the records, so > updation is also failing.
fread() and fwrite() are fully capable of reading and writing binary data. It's unlikely that they are the problem. Quote: > From the above two structures, like 'Date', which doesn't have any > nested member, it is not creating any problem. But, with a structure > like 'LoanDetail', which has an array of structures(Date), array of > doubles, etc.
Ok, you already know that you can write and read structures containing int data without problem. Do you think that reading and writing arrays is an essentially different process? Quote: > Can any one tell me the reason for fread/fwrite to fail while > writing/reading structures having arrays as members. (I have also > kept the size of the arrays as fixed, also tried with initializing all > the unused members to some null values). But, still having problem.
Without seeing the code, one would speculate that you've used an incorrect data length, passed a structure instead of a pointer to a structure (although the compiler should complain loudly about this), or failed to properly open/close the file. Quote: > Thanks, > Vijay
You're most welcome. -- Morris Dovey West Des Moines, Iowa USA C links at http://www.iedu.com/c
|
Tue, 23 Aug 2005 01:50:01 GMT |
|
|
Default Use #5 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Quote:
> Hello All, > For my project work, I am using a structure some thing like this
We heard you the first time, when you posted 20 minutes ago. This isn't a chat room, it takes time for messages to propagate out and for replies to be posted and make their way back to your server. The answer is the same as before, post a minimal complete program that demonstrates the problem. Be sure to include any compiler messages and/or the expected results versus actual results. Brian Rodenborn
|
Tue, 23 Aug 2005 02:24:25 GMT |
|
|
k.. #6 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Quote: > Hello All, > For my project work, I am using a structure some thing like this > struct Date { > int day; > int month; > int year; > }; > struct LoanDetail { > int loan_id; > struct Date payment_dates[20]; > double due_amounts[20]; > double paid_amounts[20]; > }; > I am trying to store/read/update by storing these structures in a file > on the seconday storage. To achieve, I am using fread/fwrite and also > the file is created/read/updated all in 'binary' mode. > But, I don't know, the limitations of fread/fwrite, I am having > problems in successfully adding records, reading the records, so > updation is also failing.
There's a bug in your code. In the absence of additional information, all I can guess is that it occurs on line 42, or uses `i` instead of `j` in a loop, or fails to use `sizeof` correctly. If you'd remembered to post the code, I wouldn't need to have reapplied for a clairvoyance licence. (And since I mislaid my precog licence, I don't know when the clairvoyance licence will come through.) P O S T T H E C O D E . -- Chris "electric hedgehog" Dollin C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
|
Tue, 23 Aug 2005 16:24:19 GMT |
|
|
Zoran Cutur #7 / 7
|
Problem with struct/array inside a struct using fread/fwrite
Quote: > Hello All, > For my project work, I am using a structure some thing like this > struct Date { > int day; > int month; > int year; > }; > struct LoanDetail { > int loan_id; > struct Date payment_dates[20]; > double due_amounts[20]; > double paid_amounts[20]; > }; > I am trying to store/read/update by storing these structures in a file > on the seconday storage. To achieve, I am using fread/fwrite and also > the file is created/read/updated all in 'binary' mode. > But, I don't know, the limitations of fread/fwrite, I am having > problems in successfully adding records, reading the records, so > updation is also failing. > From the above two structures, like 'Date', which doesn't have any > nested member, it is not creating any problem. But, with a structure > like 'LoanDetail', which has an array of structures(Date), array of > doubles, etc. > Can any one tell me the reason for fread/fwrite to fail while > writing/reading structures having arrays as members. (I have also > kept the size of the arrays as fixed, also tried with initializing all > the unused members to some null values). But, still having problem.
The question is, what do your fwrite/fread calls look like? They should be something along: ... struct LoanDetail record; ... if (fwrite(&record, sizeof record, 1, thestream) == 1) { /* successful write */ Quote: }
... if (fread(&record, sizeof record, 1, thestream) == 1) { /* successful read */ Quote: }
... This will be writing "record" with all its possible padding and stuff and may therefor sometime be inconvenient. --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Tue, 23 Aug 2005 16:24:21 GMT |
|
|
|