|
 |
|
 |
08-04-2005, 06:38 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 7
|
output file with system date and time
Hey guys,
I want to write output of my program in a file that includes some constant prefix like "out" concatenated with system date and time like 040820051522 for (4th Aug 2005 13:22), so the resulting output file would b something like
out040820051522.txt
i'm using is gcc on linux.
plz help.
cheers.
|
|
|
08-04-2005, 07:55 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Does this give you any idear ?
Code:
#include <stdio.h>
#include <time.h>
#define LEN 20
int main()
{
time_t now;
struct tm *t;
char file_name[LEN +1];
FILE* fp;
now = time(NULL);
t = localtime (&now);
snprintf(file_name,LEN, "out%.2d%.2d%.4d%.2d%.2d.txt",
t->tm_mday,
t->tm_mon +1,
t->tm_year +2000,
t->tm_hour,
t->tm_min);
if(!(fp=fopen(file_name, "w")))
{
printf("Error opening file: %s\n", file_name);
return -1;
}
if(!fputs("This is the line\n", fp))
{
fclose(fp);
printf("Error Writing to file: %s\n", file_name);
return -1;
}
fclose(fp);
return 0;
}
You can take a look at this thread for the full description on localtime()
|
|
|
08-04-2005, 08:16 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 7
|
brillinat !! many thanks redhed !!
|
|
|
08-04-2005, 01:13 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 7
|
though I think it should be
" t->tm_year +1900" instead of " t->tm_year +2000"
cheers
|
|
|
08-04-2005, 01:32 PM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
hmm.. perhaps a small change like:
Code:
snprintf(file_name,LEN, "out%.2d%.2d%.4d%.2d%.2d.txt",
t->tm_mday,
t->tm_mon +1,
t->tm_year > 100 ? t->tm_year +1900 : t->tm_year +2000,
t->tm_hour,
t->tm_min);
But that would fail, if the system would return it as is from year 2000, once we turn the corner of 2100 it would be 100 years off.
Then again, snprintf() isn't supported on every system..
So you would have to make a check for that if you were to distribute it onto every OS, then you'd might aswell check how the tm_year is formed.
|
|
|
08-04-2005, 01:37 PM
|
#6 (permalink)
|
|
mike
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
|
redhead, can you explain the difference between 'sprintf' and 'snprintf'? This is the first time I've seen snprintf(...). I googled it and it seems like some sort of GNU addition to C.
I see you have:
snprintf(filename, length, format string);
Is this like a safer version of sprintf? I'm trying to learn more C, especially C for Linux and it seems to me like GNU has added safer versions of things like scanf and strcpy.
- fp_unit
|
|
|
08-04-2005, 01:45 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
The difference is, that sprintf() adds to the buffer without testing for buffer overrun, with snprintf() you can tell it to only add upto LEN items to the buffer, so if you know your buffer can only contain LEN chars, then it won't fill in more, if the length of the added characters sum up to more than the LEN chars.
You don't have to google for it will tell you, the same with everyother thing gcc include libs provide for the ANSI C library.
Quote:
|
Is this like a safer version of sprintf?
|
Yes, you may say so.. it is a way to make sure theres not a possible buffer overrun exploit in your code.
|
|
|
08-04-2005, 03:44 PM
|
#8 (permalink)
|
|
mike
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
|
Ah nice. Is there any other buffer-safe versions of C functions I should look at? Does strncpy perform buffer checking compared to the non-buffer checking of strcpy? Any others?
- lowpro2k3
PS: Sorry to post off-topic.
|
|
|
08-04-2005, 04:10 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Every version that has a 'n' in it's name, will provide you with the same feature.
ie: strncpy()/strncmp()/(I'm drunk now and can't think of any other)
Theres no real off topic in this, I pointed out the use of snprintf() since ziggi pointed out using gcc on linux, since these 'n' equivalent versions are GNU based, I felt it would be safer to point out the use of it, instead of issueing a strict ANSI solution, which in this case might be bug-prone.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:26 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|