C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
please fix it Posted by laha on 3 Feb 2006 at 3:18 AM
This message was edited by laha at 2006-2-3 4:1:36

Hi
I have this function, I built 2 days ago, yesrterday it was working perfectly, today not.

char *month(int months, const char *format, char *text, size_t size)
{
   time_t t;
   if ( time ( &t ) != (time_t)(-1) )
   {
      struct tm *local = localtime ( &t );
      if ( local )
      {
         local->tm_mon += months;
         t = mktime ( local );
         if ( t != (time_t)(-1) )
         {
            local = localtime ( &t );
            if ( local )
            {
               if ( strftime ( text, size, format, local ) )
               {
                  return text;
               }
            }
         }
      }
   }
   return 0;
}

I use this function to bring the file of this month, like where are in month 2, so it suppose to bring me the file of month2 daily_report_02.txt , yesterday it brought it to me but today it suppose to bring the same file as we are still in month 2, but it brought me daily_report_03.txt, WWWWWWWWWHyyyyy

notice
one final this because I read a input file of the previous day, so the out put should be put in the same month, what I mean is if today is 1 feb so my code will read the input file of the 31/1/2006 and will put its out put in Jan (Summary_Report_01.txt).


I use this function and call it in main
( notes field contain the path)
    string outpath= field + month(-1, "Daily_Report_%d.txt", outfile, sizeof outfile) ;
ofstream outfile(outpath.c_str(), ios::out | ios::app);


please fix my error


Report
Re: please fix it Posted by stober on 3 Feb 2006 at 4:00 AM
This message was edited by stober at 2006-2-3 4:1:50

There certainly is a lot easier way to format that string than what you have done.
 char *month(int months, const char *format, char *text, size_t size)
 {
    time_t t = time(0);
    struct tm* tm = localtime(&t);
    // month is 0 based where Jan = 0, so if you want Jan = 1
    // then you have to add 1, not subtract it as you are
    // trying to do in your code.
    sprintf(text,"Daily_Report_%d.txt",tm->tm_mon+1);
    return text;
}





Report
Re: please fix it Posted by laha on 3 Feb 2006 at 4:06 AM
thank you so muc h Stober for your help, but on final think is

one final this because I read a input file of the previous day, so the out put should be put in the same month, what I mean is if today is 1 feb so my code will read the input file of the 31/1/2006 and will put its out put in Jan (Summary_Report_01.txt).

I think this will be affect by this notice.
can you help me with it please.

: This message was edited by stober at 2006-2-3 4:1:50

: There certainly is a lot easier way to format that string than what you have done.
:
:  char *month(int months, const char *format, char *text, size_t size)
:  {
:     time_t t = time(0);
:     struct tm* tm = localtime(&t);
:     // month is 0 based where Jan = 0, so if you want Jan = 1
:     // then you have to add 1, not subtract it as you are
:     // trying to do in your code.
:     sprintf(text,"Daily_Report_%d.txt",tm->tm_mon+1);
:     return text;
: }
: 

:
:
:
:
:

Report
Re: please fix it Posted by stober on 3 Feb 2006 at 4:46 AM
If you want yesterday, then subtract one from the day-of-month and call mktime() to normalize the date. If you use Microsoft compilers, the timezone settings will also affect the dates.
char *month(int months, const char *format, char *text, size_t size)
 {
    time_t t = time(0);
    struct tm* tm = localtime(&t);
    tm->tm_mday--; // yesterday
    mktime(tm);
    // month is 0 based where Jan = 0, so if you want Jan = 1
    // then you have to add 1, not subtract it as you are
    // trying to do in your code.
    sprintf(text,"Daily_Report_%d.txt",tm->tm_mon+1);
    return text;
}

Report
Re: please fix it Posted by laha on 3 Feb 2006 at 5:06 AM
Thank you Stober
it seems I did not get the whole idea.

I did a program that read bring yesterday date file and do some calculation and then put the result in an output file ( the problem is only with the out put file) I am asking

consider today 1/02/2006 I have my program will bring day_31.txt acoording to (31/01/2006) and do the some calculation, I wonder if the function you post it to me will be able to recognise that it still needs to put the out put in Summary_Report_01.txt even though we are in a new month. But when the date 2/2/2006 my own code will bring the input file day_01.txt and put the out put in a new month Daily _Report_02.txt.

the problem only with the output file.

sorry for keep asking, because I can not test it till I reach the end of the month. to see it it will recognise it.



: If you want yesterday, then subtract one from the day-of-month and call mktime() to normalize the date. If you use Microsoft compilers, the timezone settings will also affect the dates.
:
: char *month(int months, const char *format, char *text, size_t size)
:  {
:     time_t t = time(0);
:     struct tm* tm = localtime(&t);
:     tm->tm_mday--; // yesterday
:     mktime(tm);
:     // month is 0 based where Jan = 0, so if you want Jan = 1
:     // then you have to add 1, not subtract it as you are
:     // trying to do in your code.
:     sprintf(text,"Daily_Report_%d.txt",tm->tm_mon+1);
:     return text;
: }
: 

:

Report
Re: please fix it Posted by stober on 3 Feb 2006 at 7:03 AM
:
: sorry for keep asking, because I can not test it till I reach the end of the month. to see it it will recognise it.
:

simple testing solution -- change the clock on your computer.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.