How to combine string

Discussion in 'C Programming' started by Frank Hung, Oct 27, 2003.

  1. Frank Hung

    Frank Hung Guest

    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?
     
    Frank Hung, Oct 27, 2003
    #1
    1. Advertising

  2. Frank Hung

    Thomas Pfaff Guest

    (Frank Hung) writes:

    > 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 <time.h>.

    > 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.
     
    Thomas Pfaff, Oct 27, 2003
    #2
    1. Advertising

  3. Frank Hung

    Alex Guest

    "Frank Hung" <> wrote in message
    news:...
    > 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.
     
    Alex, Oct 27, 2003
    #3
  4. Thomas Pfaff <> wrote:

    > (Frank Hung) writes:
    >
    >> 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 <time.h>.
    >
    >> 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
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 27, 2003
    #4
  5. Frank Hung

    Thomas Pfaff Guest

    Irrwahn Grausewitz <> writes:

    > Thomas Pfaff <> wrote:
    >
    > > You can strcpy ".log" to buf2 before calling fopen.

    >
    > 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.
     
    Thomas Pfaff, Oct 27, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc
    Replies:
    3
    Views:
    328
    Terry Reedy
    Aug 2, 2003
  2. Squeamizh
    Replies:
    2
    Views:
    128
    Robert Klemme
    Aug 7, 2006
  3. Pat Kiatchaipipat

    combine array(string) to string?

    Pat Kiatchaipipat, Mar 20, 2008, in forum: Ruby
    Replies:
    10
    Views:
    186
    Robert Klemme
    Mar 22, 2008
  4. Todd Benson
    Replies:
    3
    Views:
    134
    Robert Klemme
    Mar 21, 2008
  5. Richard Zenn

    String Combine problem

    Richard Zenn, Sep 4, 2008, in forum: Ruby
    Replies:
    1
    Views:
    91
    Richard Zenn
    Sep 4, 2008
Loading...

Share This Page