Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Naming a file to the current date

Posted on 2003-11-21
4
408 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 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