Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

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
0
bymichelle2000
Asked:
bymichelle2000
  • 2
  • 2
1 Solution
 
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
 
bymichelle2000Author Commented:
Thanks for your help sunnycoder! ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now