please fix it - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

please fix it

lahalaha Posts: 11Member
[b][red]This message was edited by laha at 2006-2-3 4:1:36[/red][/b][hr]
Hi
I have this function, I built 2 days ago, yesrterday it was working perfectly, today not.

[code]
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;
}
[/code]
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
[code]
( 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);
[/code]

please fix my error


Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-2-3 4:1:50[/red][/b][hr]
    [blue]There certainly is a lot easier way to format that string than what you have done.[/blue]
    [code]
    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;
    }
    [/code]




  • lahalaha Posts: 11Member
    thank you so muc h Stober for your help, but on final think is
    [red]
    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).
    [/red]
    I think this will be affect by this notice.
    can you help me with it please.

    : [b][red]This message was edited by stober at 2006-2-3 4:1:50[/red][/b][hr]
    : [blue]There certainly is a lot easier way to format that string than what you have done.[/blue]
    : [code]
    : 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;
    : }
    : [/code]
    :
    :
    :
    :
    :

  • stoberstober Posts: 9,765Member ✭✭✭
    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.
    [code]
    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;
    }
    [/code]
  • lahalaha Posts: 11Member
    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.
    : [code]
    : 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;
    : }
    : [/code]
    :

  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : 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.
Sign In or Register to comment.