• C

Naming a file to the current date

Hi experts

I'm helping my company to create a log file with today's date, eg 21Nov2003.log as its file name.

Now for the date of 21Nov2003, the program has to look for 21Nov2003.log (if if exists, and continue writing at the end of file) or create a new one that extracts the current date and saves it as the file name, i.e. 21Nov2003.log

This is the code I have so far:

#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>

char day[10], mon[10], year[10], wday[10], strtime[10];
char newday[10], newmon[10], newyear[10], newwday[10],
newstrtime[10];
long curtime;

int main() {
      FILE * fp;
      FILE * newfp;
      char a[1024];
      time(&curtime);
      sscanf(ctime(&curtime),"%s %s %s %s %s",&wday,&mon,&day,&strtime,&year);
      fp=fopen ( ???, "wa" ); //<- how do I open a variable of the current date?
      if (fp != NULL) {
                fputs (a, fp); //update the file with the latest data
            fclose ( fp );
                time (&curtime);
                sscanf(ctime(&curtime),"%s %s %s %s %s",&newwday,&newmon,&newday,&newstrtime,&newyear);
                if (newday > day) {   <- got some compile error here!
            rename ( ???, ???); }//<- how do I rename the file to current date (.log extension)if the date has changed so as to update it?
 
      }
        else {
                 newfp= fopen (???,"w"); //creates a new file if it doesn't exist (with current date and .log extension)
                 fputs (a,newfp); //update the file with the latest data
                 fclose (fp);
        }
}

There's tons of error in it... hope you guys can help me out. Thanks!

bym
bymichelle2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
>how do I open a variable of the current date?
>sscanf(ctime(&curtime),"%s %s %s %s %s",&wday,&mon,&day,&strtime,&year);
>21Nov2003.log

char filename[256];

sprintf ( filename, "%s%s%s.log", day, mon,year );

fopen ( filename, "rw" );


><- got some compile error here!
what is the error message ?

>}//<- how do I rename the file to current date (.log extension)if the date has changed so as to update it?
rename ( oldfilename, newfilename);
you can form oldfilename and newfilename just as I showed above


0
bymichelle2000Author Commented:
Hi sunnycoder

>what is the error message
actually the compile error was from some other statements, which are resolved thanks to u :) but after I ran program why is it that "if (newday > day)" is still true even when it's the same day?

When I open an existing file, how do I append to writing at end of file "fp=fopen ( filename, "a+");"
and on top of that, write at a new line?

Is there a simple way of changing the month Nov to 11 and year 2003 to 03? Because the filename was truncated from 21Nov2003.log to 21Nov200.log.
What I can think of is using if (mon== Jan) mon=1, else if (mon==Feb) mon=2, if (year==2003) year = 03 and so on but as you can see, I'm having problems comparing strings... pls advise.

Thanks

bym
0
sunnycoderCommented:
> but after I ran program why is it that "if (newday > day)" is still true even when it's the same day?
both are char arrays ... you should be using strcmp or strncmp

>When I open an existing file, how do I append to writing at end of file "fp=fopen ( filename, "a+");"
>and on top of that, write at a new line?
when you open in append mode, file pointer is positioned at the end of the file and anything which you will write, wil be appended to the file ... I am not usre what you mean by "on top of that"

>Is there a simple way of changing the month Nov to 11 and year 2003 to 03? Because the filename was truncated from >21Nov2003.log to 21Nov200.log.
assuming that you cannot change the input of the format, you will have to use the if else constructs .... however since you are comparing strings, once again you have to use strcmp or strncmp

If you can change the format of the input, it would be more convenient ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bymichelle2000Author Commented:
Thanks for your help sunnycoder! ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.