-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
58 lines (51 loc) · 1.47 KB
/
ft_putnbr_fd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tpouget <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/14 17:04:37 by tpouget #+# #+# */
/* Updated: 2020/05/14 17:06:11 by tpouget ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long ft_10powerof(long n)
{
long result;
result = 1;
while (n--)
result *= 10;
return (result);
}
void ft_putnbr_fd(int n, int fd)
{
long nbr;
long digit;
long i;
long len;
char result[12];
len = 1;
nbr = n < 0 ? -1 * (long)n : n;
i = nbr;
while (i /= 10)
len++;
i = 0;
if (n < 0)
result[i++] = '-';
while (--len >= 0)
{
digit = nbr / ft_10powerof(len);
result[i++] = digit + '0';
nbr = nbr - digit * ft_10powerof(len);
}
result[i] = '\0';
write(fd, result, i);
}
/*
int main(int argc, char **argv)
{
ft_putnbr_fd(atoi(argv[1]), 2);
return 0;
}
*/