Need help creating filenames using date and time 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 7 post ] 

 Relevant Pages 

1. Get file date/time created using ANSI C

2. Q: Need algorithm to convert date-time to Julian date

3. Need help access file date times, and looping via month/years

4. help-get current date using time.h

5. C time/date to Visual Basic time/date

6. Date Time Picker - Date AND Time

7. Novice Q: need help using time

8. date and time a file is created

9. How to create a file date/time stamp

10. how to create a date-time picker control with a border

11. Unable To create month calender or List or Date Time Picker or Tab or Tree control

12. needed: C fn to find date/time of file

 

 
Powered by phpBB® Forum Software