Board index » cppbuilder » Saing a TDateTime

Saing a TDateTime

I am using a TDatePicker to input a date.  I then want to save this in a
data file ( along with other stuff).  Kinda like this:

    startdate = StartDatePicker->Date;

     // blah
     // blah

      ofstream  ofile( SaveDialog1->FileName.c_str() );
     ofile << int( startdate ) << "\n" ;

When I try to retrieve the date, I keep getting errors.

      ifstream  ifile( OpenDialog1->FileName.c_str()  );
      ifile >> startdate ;

    The error EConvertError and lists the contents of the file and states
that it is not a valid date & time.  The file looks like this:
The first line is strtdate, the rest of the lines are ints and work OK.

37260
0
0
0
.
etc

What am I doing wrong?

 

Re:Saing a TDateTime


Quote
>       ifile >> startdate ;

Hmm, looks like a bug in the VCL.  The >> operator is implemented as
follows:

  inline istream& operator >> (istream& is, TDateTime& arg)
  {
    AnsiString s;
    is >> s;
    arg = s;
    return is;
  }

That doesn't make sense.  It assigns the value of a String variable to
TDateTime, i.e. startdate = String("37260"), which is illegal.  A
workaround is to store the value in a temporary integer variable:

  int temp;
  ifile >> temp;
  startdate = temp;

Good luck!

--
Yoto Yotov

Re:Saing a TDateTime


Quote
"Yoto Yotov" <y...@NOSPAMgmx.fr> wrote in message

news:MPG.1628f5eeebbfebb99896a9@newsgroups.borland.com...

Quote
>   inline istream& operator >> (istream& is, TDateTime& arg)
>   {
>     AnsiString s;
>     is >> s;
>     arg = s;
>     return is;
>   }

> That doesn't make sense.  It assigns the value of a String variable to
> TDateTime, i.e. startdate = String("37260"), which is illegal.

No, it's not.  TDateTime has an AnsiString constructor, so when using
TDateTime's = operator with a string, a temporary TDateTime object will be
made for use in the assignment operation.  It would act like this:

    inline istream& operator >> (istream& is, TDateTime& arg)
    {
        AnsiString s;
        is >> s;
        TDateTime temp(s);
        arg = temp;
        return is;
    }

Quote
> A workaround is to store the value in a temporary integer variable:

>   int temp;
>   ifile >> temp;
>   startdate = temp;

I would opt to use a double instead of an int, as TDateTime is internally
implemented as a double.

Gambit

Re:Saing a TDateTime


Remy you hit it again.  I changed it to use a double and it solved my
problem.  I can now save & recall the date.
The save looks like this:

      ofstream  ofile( SaveDialog1->FileName.c_str() );

      double dd = startdate;
      ofile << int( dd ) << "\n" ;

Quote
"Remy Lebeau" <gambi...@gte.net> wrote in message news:3bbf51a9$2_1@dnews...
> "Yoto Yotov" <y...@NOSPAMgmx.fr> wrote in message
> news:MPG.1628f5eeebbfebb99896a9@newsgroups.borland.com...

> >   inline istream& operator >> (istream& is, TDateTime& arg)
> >   {
> >     AnsiString s;
> >     is >> s;
> >     arg = s;
> >     return is;
> >   }

> > That doesn't make sense.  It assigns the value of a String variable to
> > TDateTime, i.e. startdate = String("37260"), which is illegal.

> No, it's not.  TDateTime has an AnsiString constructor, so when using
> TDateTime's = operator with a string, a temporary TDateTime object will be
> made for use in the assignment operation.  It would act like this:

>     inline istream& operator >> (istream& is, TDateTime& arg)
>     {
>         AnsiString s;
>         is >> s;
>         TDateTime temp(s);
>         arg = temp;
>         return is;
>     }

> > A workaround is to store the value in a temporary integer variable:

> >   int temp;
> >   ifile >> temp;
> >   startdate = temp;

> I would opt to use a double instead of an int, as TDateTime is internally
> implemented as a double.

> Gambit

Re:Saing a TDateTime


Quote
> No, it's not.  TDateTime has an AnsiString constructor, so when using

Oops, I should've checked more carefully the list of constructors.  
Thanks for pointing this out.

--
Yoto Yotov

Re:Saing a TDateTime


John,

What data type is startdate, this would be important when reading in the
file? I would suggest double. In addition, I'm not sure that writing
directly to FileName.c_str() buffer is a good idea.

Andy

Quote
John Cratty wrote:

> I am using a TDatePicker to input a date.  I then want to save this in a
> data file ( along with other stuff).  Kinda like this:

>     startdate = StartDatePicker->Date;

>      // blah
>      // blah

>       ofstream  ofile( SaveDialog1->FileName.c_str() );
>      ofile << int( startdate ) << "\n" ;

> When I try to retrieve the date, I keep getting errors.

>       ifstream  ifile( OpenDialog1->FileName.c_str()  );
>       ifile >> startdate ;

>     The error EConvertError and lists the contents of the file and states
> that it is not a valid date & time.  The file looks like this:
> The first line is strtdate, the rest of the lines are ints and work OK.

> 37260
> 0
> 0
> 0
> .
> etc

> What am I doing wrong?

Other Threads