Hello,

I'm new to this and trying to understand how I should code this. I'm looking to be able to write a code where I have the string "test_this_" I'm looking to beable to add the date and time in secs at the end of the statement.

Can someone help me on this?

Thanks...

>I'm looking to beable to add the date and time in secs
Seconds from when? You'd be better off formatting the date and time, then using that string rather than trying to get some non-portable numeric formatting:

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

int main ( void )
{
  char buffer[1024];
  time_t now = time ( NULL );
  struct tm *local = localtime ( &now );

  /* Year month day hour minute second */
  size_t x = strftime ( buffer, sizeof buffer, "%Y%m%d%H%M%S", local );

  if ( x < sizeof buffer )
    puts ( buffer );

  return 0;
}

Thanks Narue,

Question am I better off just adding a number at the end of my string? Example "test_001" and increasing that number by one after every run? Because what I'm looking at is that the date and time could be the same if ran within a min of each run.

>Example "test_001" and increasing that number by one after every run?
You could certainly do that, but much like using an integer for database keys, you may have to deal with overflow eventually. Presumably by "run" you mean a new instance of the program, where you've also suddenly opened up a new problem of interprocess communication. How does a run know what number to use, and what do you do if multiple instances are running at the same time?

>Because what I'm looking at is that the date and time
>could be the same if ran within a min of each run.
The example I gave includes seconds, and with a simple check for file existence you can largely avoid clashes with other runs:

for ( ; ; ) {
  get_filename ( filename );

  if ( !exists ( filename ) ) {
    save ( filename );
    break;
  }

  sleep ( 1 );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.