LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2009, 09:11 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
name a file by the date


Hi everyone,
hope someone can help out with this, at present I write data to a debug file, however after some day this starts to get very big. basically what I do is the basic

pFile = fopen ("Debug.txt","a+");

However I'd like to open a new file every day under the date for a title. Is there a way I can create a txt file via the date ie "degug662008.txt" etc.

Thanks

Last edited by knobby67; 02-11-2009 at 09:13 AM.
 
Old 02-11-2009, 09:31 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
have you tried

char filename[20];
sprintf(filename,"debug-%i-%i-%i.txt",year,month,day);
pFile = fopen(filename,"a+");
 
Old 02-11-2009, 10:03 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
See "man strftime" for a complete example program at the end of the man page.
 
Old 02-11-2009, 10:09 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Here's an example:
Code:
/*
 * SAMPLE OUTPUT
 * Opening file debug20090211...
 */
#include <stdio.h>
#include <limits.h>
#include <time.h>

int
main (int argc, char *argv[])
{
  char fname[PATH_MAX];
  time_t t = time (NULL);
  struct tm *tm = localtime (&t);
  sprintf (fname, "debug%04d%02d%02d",
    (tm->tm_year + 1900),    /* tm_year: years since 1900 */
    (tm->tm_mon + 1),        /* tm_mon: months since January (0 .. 11) */
    tm->tm_mday );           /* tm_mday: day of month (1 .. 31) */
  printf ("Opening file %s...\n", fname);
  return 0;
}
PS:
You can also conveniently do the same thing from a shell script with the `date +FORMAT` command.
 
Old 02-11-2009, 11:28 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Personally I don't like to have different file names everyday. You can write to a file with the same name everyday and use logrotate to make sure it doesn't grow out of bounds.

jlinkels
 
Old 02-11-2009, 12:53 PM   #6
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thanks everyone.
I need to capture all data so can use logrotate.
Didn't think of sprintf.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
cp file by date chenboly Linux - General 1 04-17-2008 04:56 AM
file deletion according to date yongitz Linux - Newbie 4 07-28-2006 06:34 AM
Perl: get file date kenneho Programming 5 11-01-2005 11:16 AM
get date (file) os2 Programming 1 03-07-2005 11:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration