Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-04-2005, 06:38 AM   #1 (permalink)
ziggi
Registered User
 
Join Date: Mar 2005
Posts: 7
ziggi is on a distinguished road
Exclamation 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.
ziggi is offline   Reply With Quote
Old 08-04-2005, 07:55 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
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()
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-04-2005, 08:16 AM   #3 (permalink)
ziggi
Registered User
 
Join Date: Mar 2005
Posts: 7
ziggi is on a distinguished road
brillinat !! many thanks redhed !!
ziggi is offline   Reply With Quote
Old 08-04-2005, 01:13 PM   #4 (permalink)
ziggi
Registered User
 
Join Date: Mar 2005
Posts: 7
ziggi is on a distinguished road
though I think it should be

" t->tm_year +1900" instead of " t->tm_year +2000"

cheers
ziggi is offline   Reply With Quote
Old 08-04-2005, 01:32 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-04-2005, 01:37 PM   #6 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
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
fp_unit is offline   Reply With Quote
Old 08-04-2005, 01:45 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
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
Code:
~> man snprintf
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-04-2005, 03:44 PM   #8 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
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.
fp_unit is offline   Reply With Quote
Old 08-04-2005, 04:10 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems get local time using sys/time.h if13121 Standard C, C++ 2 10-14-2004 09:19 PM
Adjusting date() function for time zones Epsilon PHP 1 02-27-2004 02:55 AM


All times are GMT -8. The time now is 03:26 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting