Solved

Naming a file to the current date

Posted on 2003-11-21
4
401 Views
Last Modified: 2012-05-04
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
Comment
Question by:bymichelle2000
  • 2
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9795438
>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
 

Author Comment

by:bymichelle2000
ID: 9801032
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9808616
> 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
 

Author Comment

by:bymichelle2000
ID: 9809885
Thanks for your help sunnycoder! ;)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now