Quote:
Originally Posted by tronayne
Here's a little demo:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main (void)
{
time_t now;
struct tm *tm;
(void) time (&now);
tm = localtime (&now);
/* show a two-digit year */
(void) fprintf (stdout, "Today is %02d/%02d/%02d\n",
tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);
/* show a four-digit year */
(void) fprintf (stdout, "Today is %02d/%02d/%d\n",
tm->tm_mon+1, tm->tm_mday, tm->tm_year + 1900);
exit (EXIT_SUCCESS);
}
|
First of all Very Very Thanks for your reply :
But problem is somewhat different.
My Program is like Below :
with my program I am having problem with localtime() function.
so what should be the solution or what is the alternative for this localtime(), ctime(), and gmtime() so that the problem or getenv() is solved and segmentation fault problem is solved.
Please help me if possible.
/************************************************** ********************/
#include <stdio.h>
#include <string.h>
#include <utmp.h>
#include <paths.h>
#include <time.h>
struct login
{
char username[20];
char logintime[30];
char logouttime[30];
}user[100];
int main(int argc, char *argv[])
{
int i = 0;
char cTime[64] = { '\0' };
struct tm *Time;
struct utmp *entry;
utmpname(_PATH_WTMP);
setutent();
while(entry = getutent())
{
if( strcmp(entry->ut_line,":0") == 0)
{
Time = localtime(&(entry->ut_time));// with localtime the program is having problem as mentioned above
strftime(cTime,sizeof cTime,"%F %T",Time);
if(strcmp(entry->ut_user,"\0") != 0)
{
strcpy(user[i].username, "");
strcpy(user[i].logintime, "");
strcpy(user[i].username, entry->ut_user);
strcpy(user[i].logintime, cTime);
}
else
{
strcpy(user[i].logouttime, "");
strcpy(user[i].logouttime, cTime);
i++;
}
}
}
int p;
for(p = 0 ; p < i ; p++)
{
printf(" %s\t\t %s\t\t %s\n", user[p].username, user[p].logintime, user[p].logouttime);
}
endutent();
return 0;
}
/************************************************** ************************/