How to combine string

F

Frank Hung

Dear all,
I want to create file, of which name is [current date+".log"].
But i do not know how to do. My sample code is shown as bellow:

... sprintf(buf2,"%d/%d/%d %d:%d:%d",
tims->tm_mon+1,
tims->tm_mday,
tims->tm_year+1900,
tims->tm_hour,
tims->tm_min,
tims->tm_sec);

f=fopen(buf2,"a"); /* open log */->will create file "2003-10-27"

But i want to create file, "2003-10-27.log".
Can anyone teach me how to do?
 
T

Thomas Pfaff

Dear all,
I want to create file, of which name is [current date+".log"].
But i do not know how to do. My sample code is shown as bellow:

... sprintf(buf2,"%d/%d/%d %d:%d:%d",
tims->tm_mon+1,
tims->tm_mday,
tims->tm_year+1900,
tims->tm_hour,
tims->tm_min,
tims->tm_sec);

See strftime in said:
f=fopen(buf2,"a"); /* open log */->will create file "2003-10-27"
But i want to create file, "2003-10-27.log".

You can strcpy ".log" to buf2 before calling fopen.
 
A

Alex

Frank Hung said:
Dear all,
I want to create file, of which name is [current date+".log"].
But i do not know how to do. My sample code is shown as bellow:

... sprintf(buf2,"%d/%d/%d %d:%d:%d",
tims->tm_mon+1,
tims->tm_mday,
tims->tm_year+1900,
tims->tm_hour,
tims->tm_min,
tims->tm_sec);

f=fopen(buf2,"a"); /* open log */->will create file "2003-10-27"

Not according to the format in the sprintf call.
But i want to create file, "2003-10-27.log".
Can anyone teach me how to do?

Just specify the appropriate format in the call to sprintf:

sprintf(buf2, "%4d-%02d-%02d.log", tims->tm_year + 1900,
tims->tm_mon + 1, tims->tm_mday);
f = fopen(buf2, "a");

Better still, use strftime() in much the same way.
 
I

Irrwahn Grausewitz

Thomas Pfaff said:
[email protected] (Frank Hung) said:
Dear all,
I want to create file, of which name is [current date+".log"].
But i do not know how to do. My sample code is shown as bellow:

... sprintf(buf2,"%d/%d/%d %d:%d:%d",
tims->tm_mon+1,
tims->tm_mday,
tims->tm_year+1900,
tims->tm_hour,
tims->tm_min,
tims->tm_sec);

See strftime in said:
f=fopen(buf2,"a"); /* open log */->will create file "2003-10-27"
But i want to create file, "2003-10-27.log".

You can strcpy ".log" to buf2 before calling fopen.

Better use strcat. ;-)
Or append ".log" to the sprintf format string.

Regards
 
T

Thomas Pfaff

Irrwahn Grausewitz said:
Better use strcat. ;-)

Since strftime returns the number of characters written to the destination,
you might aswell use strcpy and save the time needed to discover something
you already know.

But I won't claim this is what I had in mind in my first post. I did mean
to write strcat ;-) Thanks.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top