Bore Biko wrote On 09/20/05 17:22,:
Sshh! Not in front of the children!
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
"It doesn't work correctly" is no more informative
than "It's wrong." You haven't described what's wrong,
why you think it's wrong, or how it differs from what
you expected. I'll try to guess, but if I'm attacking
the wrong problem you have only yourself to blame.
Here s code compiled with gcc
... but with the warnings muted, or else gcc
would have issued complaints. (In fact, it should
have complained anyhow; are you sure you've shown
us exactly the same code you've been compiling?)
For nearly all code I'd recommend using
"gcc -W -Wall". For highly portable code, go even
further to "gcc -W -Wall -ansi -pedantic" (or you
might replace "-ansi" with "-std=whatever").
it goes (if you name program as "transform"),
transform 1000, for 1000 seconds...
#include<stdio.h>
You forgot to #include said:
void transform(char*, int);
int main(int argc, char* argv[]){
int time;
char formated_t[12];
time=atoi(argv[1]);
It might have been wise to make sure argv[1] is
actually present before trying to convert it ... A
quick look at the value of argc would tell the tale.
Also, atoi() is not a very good way to convert
strings to numbers. The problem is that it cannot be
relied upon to do anything sensible if the input is
something like "zaphod" instead of a numeric string.
It also can't be relied upon if the input is something
along the lines of "99999999999999999999999999999999".
Functions like strtol() are much safer.
transform(&formated_t, time);
Here's what the compiler should have complained
about: the function's first argument is supposed to
be a pointer to a `char', but you're passing a pointer
to an array of `char' instead. If you don't understand
the difference, see Question 6.12 in the comp.lang.c
Frequently Asked Questions (FAQ) list
http://www.eskimo.com/~scs/C-faq/top.html
printf("\n%s", formated_t);
A subtle bug that might not bother you at the
moment but that will (by Murphy's Law) bite you at
some other, more important moment: The last character
sent to a text output stream before it is closed
must be a '\n', or the final line might never appear
or might appear in some muddled way. Some systems will
let you get away with this; others will not.
You have (correctly) defined main() as returning
an `int' value, but you haven't actually returned one.
There's a special dispensation for this in the latest
version of the C Standard, but more compilers hew to
the older version than to the new one, so it's unwise
to rely on it as yet.
}
void transform(char t[], int time){
int days, hours, minuts, seconds, i, j, k;
days=time/(24*3600);
Here's another subtle trap, less common nowadays
than it used to be, but still occasionally stumbled
upon like a forgotten land mine. `24' and `3600' are
`int' constants, so their product will be computed as
an `int'. However, the range of `int' varies from one
compiler to the next, and its upper limit can be as low
as 32767. Since the product 86400 is well above this
limit, the computation could overflow. For perfect
safety, you should carry it out in `long' instead.
All right: `i' is now the number of seconds left
over once you've taken out the days.
... but what's this? You're adding the number of
days -- not the number of seconds in those days, just
the days themselves -- to the number of left-over seconds.
You then pursue the same pattern in the rest of the code,
but the pattern itself is nonsensical. Seventy-one seconds
is one minute ten seconds, not one minute eleven. Work a
few of these conversions with pencil and paper, study the
steps you take, and reproduce those steps in code.
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
i=time%60;
time=time/60+i;
seconds= time % 60;
sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);
Two problems here. First, if the original number of
seconds is large enough you'll generate more characters than
will fit in the space allotted to `t'. 8726399 seconds would
be 100:23:59:59, which (with the terminating '\0') would try
to put thirteen characters in a twelve-character sack, with
unpredictable consequences.
Second, the plain "%d" conversion will not always generate
the two digits you desire. You'll get things like 1:13:0:59:1
instead of 01:13:00:59:01. To force two positions, use "%2d".
To fill with leading zeroes instead of leading blanks, use
"%02d".