Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi all, I am trying to use the Cti

Status
Not open for further replies.

logic4fun

Programmer
Apr 24, 2003
85
US
Hi all,
I am trying to use the Ctime function and it works fine when i use that in two different lines but it wont when i use two Ctime calls in one line..confusing..donno why..here is a snippet which shows that..

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <sys/timeb.h>

int main(int argc, char** argv )
{
long lt;
time_t junk;
int i;
time_t blah;


for (i=0 ; i<10 ; i++) {
lt = time(&junk) ;
blah = lt + 10;
printf(&quot;LongTime is %ld,%ld,%s,%s \n&quot;,lt,blah,ctime(&lt),ctime(&blah));

printf(&quot;%s \n&quot;,ctime(&blah));
sleep(1);
}
return 0;
}

In the above what happens is it will return the same time for both the function calls if they are made on the same line..though the arguments to that call are different..Any suggestions..

here is the result:
LongTime is 1069433566,1069433576,Fri Nov 21 11:52:46 2003
,Fri Nov 21 11:52:46 2003

Fri Nov 21 11:52:56 2003

Thanks in advance
Logic4Fun
 
This happens because what ctime() returns is a pointer to the formatted string. It is the same pointer for both calls to ctime. You are simply printing the same string twice. Just do 2 separate statements.

LOL!
 
Well first of all the code snipette posted by you does not compile. Anyways, ctime will format a string based on the input. If you supply same input to multiple calls to ctime() you will get same output string. It does not matter at all if it is in same line or in a different line.

Can you post your working code which gives wrong output.

 
The code compiles fine, the problem isn't with the code, but with how the code displays. Not sure how to get it to display correctly when posted to this forum, maybe someone with a mind infinitely superior to mine could enlighten us.

I'll change your lt variable to llt, then it displays correctly (lt is the html tag for less than or <)

Then your printf statement is:
Code:
   printf(&quot;LongTime is %ld,%ld,%s,%s \n&quot;,llt,blah,ctime(&llt),ctime(&blah));

Anyway, I stand by my previous answer, sorry I didn't explain it better. To illustrate the concept of what is happening with ctime(), execute this code:
Code:
#include <stdio.h>
#include <string.h>

char *teststr(char *s) {

  static char sloc[15];

  strcpy(sloc,s);
  return(sloc);
}

int main(int argc, char **argv )
{
  printf(&quot;1:%s 2:%s\n&quot;,teststr(&quot;string-1&quot;),teststr(&quot;string-2&quot;));
  return 0;
}
You'll see that it prints &quot;string-1&quot; twice because the function returns a pointer to the static string sloc which is the same pointer every time the function is called.
Something similar is happening with the ctime() function.

If I modify the code to do the print in 2 statements
Code:
  printf(&quot;1:%s &quot;,teststr(&quot;string-1&quot;));
  printf(&quot;2:%s\n&quot;,teststr(&quot;string-2&quot;));
Now it works fine.

Hope this helps.
 
Oh OK... now I understood your problem... You are very right in judging the problem and telling that it returns pointer to some static object. Actually that is what the behaviour of ctime is expected to be. See this is from the man page of ctime >>>>

The ctime(), asctime(), gmtime(), and localtime() functions return values in one of two static objects: a broken-down time structure and an array of char. Execution of any of the functions may overwrite the information returned in either of these objects by any of the other functions. <<<<<


That means its not just ctime there are some more culprit functions that have similar behaviour. You can use ctime_r() instead.

I am posting clipping of its man pages also.

>>>>
The ctime_r() function has the same functionality as ctime() except that the caller must supply a buffer buf with length buflen to store the result; buf must be at least 26
bytes. The POSIX ctime_r() function does not take a buflen parameter. <<<<


Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top