BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using TFileStream to Save a Structure Containing an Array

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Del Ventruella
Guest





PostPosted: Wed Jun 15, 2005 12:48 pm    Post subject: Using TFileStream to Save a Structure Containing an Array Reply with quote




How does one use TFileStream in Borland Builder 6.0 to save and then recall a structure FILE containing an array?

For Example, how would I save and then recall "ArraysOfNums" below?:

Struct ArraysOfNums
{
double A[28];
double B[28];
};

Thanks in advance to whomever might take a moment to clear this up for me.

Sincerely,

Del Ventruella
Back to top
Liz Albin
Guest





PostPosted: Wed Jun 15, 2005 2:15 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote



On 15 Jun 2005 05:48:52 -0700, Del Ventruella wrote:

Quote:
Struct ArraysOfNums
{
double A[28];
double B[28];
};

for one thing, it would be struct, not Struct

Check the ReadBuffer() and WriteBuffer() methods,

in ether case, Count will be something like

int Count = sizeof(ArrayOfNums);
--
Good luck,

liz

Back to top
Del Ventruella
Guest





PostPosted: Wed Jun 15, 2005 2:59 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote




"Del Ventruella" <ventruella (AT) hotmail (DOT) com> wrote:
Quote:

How does one use TFileStream in Borland Builder 6.0 to save and then recall a structure FILE containing an array?

For Example, how would I save and then recall "ArraysOfNums" below?:

Struct ArraysOfNums
{
double A[28];
double B[28];
};

Thanks in advance to whomever might take a moment to clear this up for me.

Sincerely,

Del Ventruella


I have tried the following using the ReadBuffer and WriteBuffer methods, but with not success in saving the structures of arrays of doubles:

TStream* stream1=new TFileStream(NameOfFile, fmCreate | fmShareDenyNone);
stream1->WriteBuffer((char*)(&*ArrayOfNums), sizeof(ArrayOfNums));

AND

TStream* stream1=new TFileStream(filename, fmOpenRead | fmShareDenyNone);
stream1->ReadBuffer((char*)(&*ArrayOfNums), sizeof(ArrayOfNums));

I am trying to cast the address of the first element of the structure (ArrayOfNums) as a char pointer and get the code to read (or write) to the end of the structure based upon the size of the structure. There may be a much easier approach employing a more straightforward application of a buffer, but translating a structure containing arrays of doubles such as ArrayOfNums into a buffer seems to require a bit more than doing the same for an AnsiString.

I am blanking right now in that regard.

Thanks for any effort to point me in the right direction.

Sincerely,

Del Ventruella

Back to top
Liz Albin
Guest





PostPosted: Wed Jun 15, 2005 3:04 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote

On 15 Jun 2005 07:59:44 -0700, Del Ventruella wrote:

Quote:
stream1->WriteBuffer((char*)(&*ArrayOfNums), sizeof(ArrayOfNums));

please give your exact code and say exactly what errors you're gotten.
--
Good luck,

liz

Back to top
Alan Bellingham
Guest





PostPosted: Wed Jun 15, 2005 3:51 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote

"Del Ventruella" <ventruella (AT) hotmail (DOT) com> wrote:

Quote:
For Example, how would I save and then recall "ArraysOfNums" below?:

Struct ArraysOfNums
{
double A[28];
double B[28];
};

I have tried the following using the ReadBuffer and WriteBuffer methods, but with not success in saving the structures of arrays of doubles:

(Please consider keeping your line length below the 80 mark.)

Quote:
stream1->WriteBuffer((char*)(&*ArrayOfNums), sizeof(ArrayOfNums));

Err, you expect this to work? ArraysOfNums is a _type_ - in other words,
there may be any one of a number of them on the system. They'll all be
the same size (which means that your sizeof() will work), but how is the
code to guess which instance (if any) to save?

You need to declare an actual instance first:

ArrayOfNums MyArray;
// ...

and then you will pass the address of the instance:

stream1->WriteBuffer(&MyArray, sizeof MyArray);

BTW - your cast to char* was completely unnecessary, and though not
actually dangerous, inadvisable, since the WriteBuffer method actually
takes a 'void const *'.

I'll leave the read logic to you, since it's almost identical.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK

Back to top
Del Ventruella
Guest





PostPosted: Wed Jun 15, 2005 4:03 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote


Alan Bellingham <alanb (AT) episys (DOT) com> wrote:
Quote:
"Del Ventruella" <ventruella (AT) hotmail (DOT) com> wrote:

For Example, how would I save and then recall "ArraysOfNums" below?:

Struct ArraysOfNums
{
double A[28];
double B[28];
};

I have tried the following using the ReadBuffer and WriteBuffer methods, but with not success in saving the structures of arrays of doubles:

(Please consider keeping your line length below the 80 mark.)

stream1->WriteBuffer((char*)(&*ArrayOfNums), sizeof(ArrayOfNums));

Err, you expect this to work? ArraysOfNums is a _type_ - in other words,
there may be any one of a number of them on the system. They'll all be
the same size (which means that your sizeof() will work), but how is the
code to guess which instance (if any) to save?

You need to declare an actual instance first:

ArrayOfNums MyArray;
// ...

and then you will pass the address of the instance:

stream1->WriteBuffer(&MyArray, sizeof MyArray);

BTW - your cast to char* was completely unnecessary, and though not
actually dangerous, inadvisable, since the WriteBuffer method actually
takes a 'void const *'.

I'll leave the read logic to you, since it's almost identical.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK


I'll try again. (I left the creation of the instance of the structure out of the prior post, but it is in my code.) Most importantly, Thank you.

Sincerely,
Del Ventruella


Back to top
Liz Albin
Guest





PostPosted: Wed Jun 15, 2005 4:39 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote

On 15 Jun 2005 09:03:30 -0700, Del Ventruella wrote:

Quote:
I'll try again. (I left the creation of the instance of the structure
out of the prior post, but it is in my code.) Most importantly, Thank
you.

Please wrap your lines.

The reason I asked for exact code was that it was completely unclear
whether ArrayOfNum was a type or an instance.

So, please show the code that doesn't work, and explain precisely /what/
doesn't work.


--
Good luck,

liz

Back to top
Sergiy Kanilo
Guest





PostPosted: Wed Jun 15, 2005 7:11 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote


"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote

Quote:
"Del Ventruella" <ventruella (AT) hotmail (DOT) com> wrote:

For Example, how would I save and then recall "ArraysOfNums" below?:

Struct ArraysOfNums
{
double A[28];
double B[28];
};

I have tried the following using the ReadBuffer and WriteBuffer methods,
but with not success in saving the structures of arrays of doubles:

(Please consider keeping your line length below the 80 mark.)

stream1->WriteBuffer((char*)(&*ArrayOfNums), sizeof(ArrayOfNums));

Err, you expect this to work? ArraysOfNums is a _type_ - in other words,

ArraysOfNums is a type, ArrayOfNums could be anything

My gues is that ArrayOfNums probably _a pointer_ to ArraysOfNums
and problem with saving is that sizeof of pointer is not correct size of the
saved data

Cheers,
Serge



Back to top
Alan Bellingham
Guest





PostPosted: Thu Jun 16, 2005 12:49 am    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote

"Sergiy Kanilo" <skanilo (AT) artannlabs (DOT) com> wrote:

Quote:
ArraysOfNums is a type, ArrayOfNums could be anything

Ah, not obvious. In fact, if the OP was trying to hide what was going
on, that would be a good way to do it.

Quote:
My gues is that ArrayOfNums probably _a pointer_ to ArraysOfNums
and problem with saving is that sizeof of pointer is not correct size of the
saved data

It depends. Is it a compilation error, or a runtime error?

Alan Bellingham
--
Me <url:mailto:alanb (AT) episys (DOT) com> <url:http://www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:http://accu.org/>
The 2004 Discworld Convention <url:http://dwcon.org/>

Back to top
Sergiy Kanilo
Guest





PostPosted: Thu Jun 16, 2005 1:14 am    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote


"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote

Quote:
"Sergiy Kanilo" <skanilo (AT) artannlabs (DOT) com> wrote:

ArraysOfNums is a type, ArrayOfNums could be anything

Ah, not obvious. In fact, if the OP was trying to hide what was going
on, that would be a good way to do it.

My gues is that ArrayOfNums probably _a pointer_ to ArraysOfNums
and problem with saving is that sizeof of pointer is not correct size of
the
saved data

It depends. Is it a compilation error, or a runtime error?

Probably there is no error, otherwise OP mentioned it.
Insted he said "I have tried the following ... but with not success in
saving the structures of ...".
If ArrayOfNums is a pointer, that is exactly what should happen.

Could be that in previous version of the code, ArrayOfNums was an automatic
or static array of structures (sizeof(ArrayOfNums) gave correct size),
but later the array was replaced with a pointer and memory allocation, and
code failed in saving the data.
Of course, it is just speculation, but I believe it is a very common
situation.

Cheers,
Serge



Back to top
Alan Bellingham
Guest





PostPosted: Thu Jun 16, 2005 8:54 am    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote

"Sergiy Kanilo" <skanilo (AT) artannlabs (DOT) com> wrote:

Quote:
It depends. Is it a compilation error, or a runtime error?

Probably there is no error, otherwise OP mentioned it.
Insted he said "I have tried the following ... but with not success in
saving the structures of ...".

Hmm, and I thought he meant that he couldn't work out how to get it to
compile. (The 'with not success' I interpreted to be an error at
compilation stage, since what was provided was apparently uncompilable.)

Alan Bellingham
--
Me <url:mailto:alanb (AT) episys (DOT) com> <url:http://www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:http://accu.org/>
The 2004 Discworld Convention <url:http://dwcon.org/>

Back to top
Liz Albin
Guest





PostPosted: Thu Jun 16, 2005 1:00 pm    Post subject: Re: Using TFileStream to Save a Structure Containing an Arra Reply with quote

On Thu, 16 Jun 2005 09:54:59 +0100, Alan Bellingham wrote:

Quote:
Hmm, and I thought he meant that he couldn't work out how to get it to
compile. (The 'with not success' I interpreted to be an error at
compilation stage, since what was provided was apparently uncompilable.)

Same here -- starting with the Struct declaration -- which is also why I
was completely unsure whether he was using a type or an instance
--
Good luck,

liz

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.