[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Re: reading / writing a string

Per

12/3/2008 12:01:00 PM

On 2008-12-03, hamishd <Hamish.Dean@gmail.com> wrote:
> Hi, I have a structure like this:
>
> #define MAX_LENGTH 20
>
> struct InformationClass
> {
> char StringA[MAX_LENGTH];
> char StringB[MAX_LENGTH];
> };
>
> I want to be able to write it to a file, and also read it back in
> again? I am using fprintf, and fscanf.
>
> Write like this:
> fprintf(stream, "%s%s", IC.StringA, IC.StringB);
>
> Read like this:
> fscanf(stream, "%s%s", &IC.StringA, &IC.StringB);
>
> It doesn't seem to work. Is there an easy way to do this?
>

To me this looks like C-code. In C++ I would have overloaded the <<
and >> operators for istream and ostream respectively. If its just
serialization you are after there is also the boost serialization library.

Further in C++ use std::string instead of char[].

/Per