rename according to time

B

byte

hi C programmers,
im a c newbie.
i would like to rename an existing file(xxx.7z) according to time()
and
localtime() and get the outcome like 0127172718.7z
(month:day:hour:min:seconds with suffix name)
i am not sure how to do this in c.
please help.
p.s:i hope run this code under xp
 
J

Jensen Somers

byte said:
hi C programmers,
im a c newbie.
i would like to rename an existing file(xxx.7z) according to time()
and
localtime() and get the outcome like 0127172718.7z
(month:day:hour:min:seconds with suffix name)
i am not sure how to do this in c.
please help.
p.s:i hope run this code under xp

Your operating system doesn't matter, it depends how you compile it.
Using Visual Studio, using lcc-win, using mingw, ...

Take a look at the rename() function included in <stdio.h>.

- Jensen
 
M

Mark Bluemel

byte said:
hi C programmers,
im a c newbie.
i would like to rename an existing file(xxx.7z) according to time()
and
localtime() and get the outcome like 0127172718.7z
(month:day:hour:min:seconds with suffix name)
i am not sure how to do this in c.

I'm inclined to believe you can't do this in standard C - you'll need to
use operating-system specific functionality.
please help.
p.s:i hope run this code under xp

I suggest you ask in a Windows programming group for the OS-specifics.

You could also look at msdn -
http://msdn2.microsoft.com/en-us/library/aa365239(VS.85).aspx looks
relevant, but I'm not a Windows programmer so can't comment much.
 
A

Army1987

byte said:
hi C programmers,
im a c newbie.
i would like to rename an existing file(xxx.7z) according to time()
and
localtime() and get the outcome like 0127172718.7z
(month:day:hour:min:seconds with suffix name)
i am not sure how to do this in c.
Look up strftime() and rename().
 
R

Richard Heathfield

Mark Bluemel said:
I'm inclined to believe you can't do this in standard C - you'll need to
use operating-system specific functionality.

What's wrong with strftime, Mark?
 
E

erfan

Your operating system doesn't matter, it depends how you compile it.
Using Visual Studio, using lcc-win, using mingw, ...

Take a look at the rename() function included in <stdio.h>.

- Jensen
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
srand();
rename(*old name,rand(time(null)));
 
M

Mark Bluemel

Mark said:
I'm inclined to believe you can't do this in standard C - you'll need to
use operating-system specific functionality.

Doh. I need to read the up-to-date standard more carefully. I'm still
inclined to think K&R (classic).
 
M

Mark Bluemel

Richard said:
Mark Bluemel said:


What's wrong with strftime, Mark?

As my other followup indicates, I learnt C from the original K&R book...

Add to that my inclination to work on Unix/Linux pretty much
exclusively, and to use (consciously) a lot of Posix functionality, I
have to confess to sometimes not realising how much is now part of the
standard.

Sorry!
 
V

vippstar

#include<time.h>
#include<stdio.h>
#include<stdlib.h>
srand();
rename(*old name,rand(time(null)));
What is that code supposed to do?
It's not a valid C snip.
 
J

Jensen Somers

What is that code supposed to do?
It's not a valid C snip.

Does it have to be?

It's just a basic illustration of what should be done. The topic starter
should be able to figure his steps out from this code.

- Jensen
 
J

Joachim Schmitz

Jensen said:
Does it have to be?

It's just a basic illustration of what should be done. The topic
starter should be able to figure his steps out from this code.
Well, rand doesn't take an argument (srand does though) and returns an int,
rename takes 2 char *. So your code isn't even good pseudo code.

Furthermore a filename generated with rand doesn't have anything to do with
the OP's request, who wanted filenames containg date and time, rather than a
pseudo random number, so vippstar's "code" wasn't even close.

Bye, Jojo
 
P

Philip Potter

Joachim said:
Well, rand doesn't take an argument (srand does though) and returns an int,
rename takes 2 char *. So your code isn't even good pseudo code.

Furthermore a filename generated with rand doesn't have anything to do with
the OP's request, who wanted filenames containg date and time, rather than a
pseudo random number, so vippstar's "code" wasn't even close.

It was erfan, not vippstar, who wrote it.
 
B

Bill Reid

Richard Heathfield said:
Mark Bluemel said:


What's wrong with strftime, Mark?

Nothing, except by the time you plow through all the billions of
cryptic conversion specifiers to figure out how to format the strftime()
string, you could have just used sprintf() to print your own string from
the tm structure about seven times over...

But in any event, as is now very clear, this is one of the rare
things that can actually be accomplished in "standard" C...
 
G

Geoff

hi C programmers,
im a c newbie.
i would like to rename an existing file(xxx.7z) according to time()
and
localtime() and get the outcome like 0127172718.7z
(month:day:hour:min:seconds with suffix name)
i am not sure how to do this in c.
please help.
p.s:i hope run this code under xp

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

// stuff a string with today's date
// adding hours, mins, secs left as exercise for student
void make_time_string (void)
{
time_t long_time;
struct tm *ltime;
char string[20];

time(&long_time);
ltime = localtime(&long_time);

sprintf(string, "%02i%02i%02i",
ltime->tm_year + 1900,
ltime->tm_mon + 1,
ltime->tm_mday);
}

Compiles OK in VC++ 6.0, functionality not tested.
 
S

stmilam

hi C programmers,
im a c newbie.
i would like to rename an existing file(xxx.7z) according to time()
and
localtime() and get the outcome like 0127172718.7z
(month:day:hour:min:seconds with suffix name)
i am not sure how to do this in c.
please help.
p.s:i hope run this code under xp

You mean something like this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/
**********************************************************************/
/* Name:
*/
/* mk_timestamped_filename() - Return a timestamped filename.
*/
/*
*/
/* Synopsis:
*/
/* #include "dirtools.h"
*/
/* int
mk_timestamped_filename( */
/* char *dest,
*/
/* char *filename,
*/
/* char *extension
*/
/* );
*/
/*
*/
/* Description:
*/
/* The mk_timestamped_filename() function uses the filename and
*/
/* extension arguments to construct a filename with a date/time
*/
/* stamp as a component of the filename. E.G.: If the filename
*/
/* is "ABC" and the extension is "DEF" the resulting filename
*/
/* would be "ABC.YYYYMMDDMMSS.DEF" where YYYY is the year, MM is
*/
/* the month, DD is the day of the month, MM is the minute, and
*/
/* SS is the second.
*/
/*
*/
/* Arguments:
*/
/* char *dest - The area memory where the timestamped file
*/
/* name will be constructed. Be sure to allow
*/
/* enough space to build the full filename!
*/
/* char *filename - The leading component of filename.
*/
/* char *extension - An optional extension that will follow the
*/
/* the timestamp. If NULL or an empty string
*/
/* is used this component of the file name will
*/
/* be omitted.
*/
/*
*/
/* Return Value:
*/
/* The address of the dest argument.
*/
/*
*/
/* See Also:
*/
/* uniqname().
*/
/*
*/
/* Example:
*/
/* #include <stdio.h>
*/
/* #include "dirtools.h"
*/
/*
*/
/* int
*/
/* main( int argc, char **argv )
*/
/*
{ */
/* char dest[FILENAME_MAX+1];
*/
/* char *extension = argc < 3 ? "" : argv[2];
*/
/* char *filename = argc < 2 ? "fubar" : argv[1];
*/
/*
*/
/*
puts( */
/*
mk_timestamped_filename( */
/* dest,
*/
/* filename,
*/
/* extension
*/
/* )
*/
/* );
*/
/* return 0;
*/
/* }
*/
/*
*/
/
**********************************************************************/

char *
mk_timestamped_filename( char *dest, char *filename, char *extension )
{
time_t tv;
struct tm tm;
char wrkbuf[FILENAME_MAX + 1] = "";

/
******************************************************************/
/* Build the time related component of the file.
*/
/
******************************************************************/

tv = time( NULL );
tm = *localtime( &tv );
strftime( wrkbuf, sizeof(wrkbuf), "%Y%m%d%H%M%S", &tm );

/
******************************************************************/
/* Use sprintf() to build the full filename with or without the
*/
/* extension.
*/
/
******************************************************************/

sprintf(
dest,
extension == NULL || *extension == 0 ? "%s.%s" : "%s.%s.%s",
filename,
wrkbuf,
extension
);

return dest;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top