Need help creating filenames using date and time
Author |
Message |
kjohn.. #1 / 7
|
 Need help creating filenames using date and time
Hi, I'm trying to create filenames using day of year and time such as 298140132.log. I'm taking the date and time integers from a tm struct and trying to cast them to strings and concatenate them into the desired file name. However, when I compile the following code: #include <stdio.h> #include <time.h> int main(void) { time_t CurrentTime; struct tm now; char day[4], hour[3], min[3], sec[3]; CurrentTime = time(NULL); strcpy(time_string, ctime(&CurrentTime)); now = *localtime(&CurrentTime); // ALL'S FINE UP TO HERE strcpy(day, (char)now.tm_yday); printf("Day is %s\n", day); : : : I get the following warning: warning: passing arg 2 of 'strcpy' makes pointer of integer without a cast Am I trying to do this the impossible way? Any help will be greatly appreciated. Kent
|
Sat, 13 Apr 2002 03:00:00 GMT |
|
 |
Charles LaCou #2 / 7
|
 Need help creating filenames using date and time
| Hi, | | I'm trying to create filenames using day of year and time such as | 298140132.log. I'm taking the date and time integers from a tm struct | and trying to cast them to strings and concatenate them into the | desired file name. However, when I compile the following code: | | #include <stdio.h> | #include <time.h> | | int main(void) | { | | time_t CurrentTime; | struct tm now; | char day[4], hour[3], min[3], sec[3]; | | CurrentTime = time(NULL); | strcpy(time_string, ctime(&CurrentTime)); | now = *localtime(&CurrentTime); | | // ALL'S FINE UP TO HERE | | strcpy(day, (char)now.tm_yday); now.tm_yday is not a string and casting it to a *char* doesn't change that situation. What you really want is sprintf( day, "%d", now.tm_yday ); | | printf("Day is %s\n", day); | : | : | : | I get the following warning: | | warning: passing arg 2 of 'strcpy' makes pointer of integer without a | cast | | Am I trying to do this the impossible way? Any help will be greatly | appreciated. | | Kent
|
Sat, 13 Apr 2002 03:00:00 GMT |
|
 |
John Gord #3 / 7
|
 Need help creating filenames using date and time
Quote:
> #include <stdio.h> > #include <time.h> > int main(void) > { > time_t CurrentTime; > struct tm now; > char day[4], hour[3], min[3], sec[3]; > CurrentTime = time(NULL); > strcpy(time_string, ctime(&CurrentTime));
this code can't possibly compile. "time_string" is undeclared. Quote: > now = *localtime(&CurrentTime); > strcpy(day, (char)now.tm_yday);
now.tm_yday is an int. you can't strcpy() from an int. and that cast does nothing but cloud the issue. if you want to convert from an int to a string, use sprintf(): sprintf(day, "%s", now.tm_yday); --- John Gordon "No Silicon Heaven? Preposterous! Where would
|
Sat, 13 Apr 2002 03:00:00 GMT |
|
 |
Robert Stankowi #4 / 7
|
 Need help creating filenames using date and time
Kent, you should have a look at the tm - struct's member types. I dont recall at the moment, but I think tm.tm_yday is an int, so you will have to convert it to a character string before using strcat. sprintf will do the job good luck Robert Quote:
> Hi, > I'm trying to create filenames using day of year and time such as > 298140132.log. I'm taking the date and time integers from a tm struct > and trying to cast them to strings and concatenate them into the > desired file name. However, when I compile the following code: > #include <stdio.h> > #include <time.h> > int main(void) > { > time_t CurrentTime; > struct tm now; > char day[4], hour[3], min[3], sec[3]; > CurrentTime = time(NULL); > strcpy(time_string, ctime(&CurrentTime)); > now = *localtime(&CurrentTime); > // ALL'S FINE UP TO HERE > strcpy(day, (char)now.tm_yday); > printf("Day is %s\n", day); > : > : > : > I get the following warning: > warning: passing arg 2 of 'strcpy' makes pointer of integer without a > cast > Am I trying to do this the impossible way? Any help will be greatly > appreciated. > Kent
|
Sun, 14 Apr 2002 03:00:00 GMT |
|
 |
Lawrence Kir #5 / 7
|
 Need help creating filenames using date and time
Quote:
>> #include <stdio.h> >> #include <time.h> >> int main(void) >> { >> time_t CurrentTime; >> struct tm now; >> char day[4], hour[3], min[3], sec[3]; >> CurrentTime = time(NULL); >> strcpy(time_string, ctime(&CurrentTime)); >this code can't possibly compile. "time_string" is undeclared. >> now = *localtime(&CurrentTime); >> strcpy(day, (char)now.tm_yday); >now.tm_yday is an int. you can't strcpy() from an int. and that >cast does nothing but cloud the issue. if you want to convert from
Right, char is just another integer type. Quote: >an int to a string, use sprintf(): > sprintf(day, "%s", now.tm_yday);
That needs to be %d. -- -----------------------------------------
-----------------------------------------
|
Sun, 14 Apr 2002 03:00:00 GMT |
|
 |
kjohn.. #6 / 7
|
 Need help creating filenames using date and time
Thanks guys! You've all helped me out a great deal. Kent Quote:
>Hi, >I'm trying to create filenames using day of year and time such as >298140132.log. I'm taking the date and time integers from a tm struct >and trying to cast them to strings and concatenate them into the >desired file name. However, when I compile the following code: >#include <stdio.h> >#include <time.h> >int main(void) >{ >time_t CurrentTime; >struct tm now; >char day[4], hour[3], min[3], sec[3]; >CurrentTime = time(NULL); >strcpy(time_string, ctime(&CurrentTime)); >now = *localtime(&CurrentTime); >// ALL'S FINE UP TO HERE >strcpy(day, (char)now.tm_yday); >printf("Day is %s\n", day); >: >: >: > I get the following warning: >warning: passing arg 2 of 'strcpy' makes pointer of integer without a >cast >Am I trying to do this the impossible way? Any help will be greatly >appreciated. >Kent
|
Sun, 14 Apr 2002 03:00:00 GMT |
|
 |
Paul D. Boy #7 / 7
|
 Need help creating filenames using date and time
: I'm trying to create filenames using day of year and time such as : 298140132.log. I'm taking the date and time integers from a tm struct : and trying to cast them to strings and concatenate them into the : desired file name. What you want to do is use the strftime() (especially read about the %j conversion) and sprintf(). Try writing some code with these two functions. If you still have problems post your code here. Paul -- Paul D. Boyle
North Carolina State University http://laue.chem.ncsu.edu/web/xray.welcome.html
|
Sun, 14 Apr 2002 03:00:00 GMT |
|
|
|