-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_putnbr_fd.c
More file actions
72 lines (63 loc) · 1.61 KB
/
ft_putnbr_fd.c
File metadata and controls
72 lines (63 loc) · 1.61 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bhielsch <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/13 17:22:55 by bhielsch #+# #+# */
/* Updated: 2022/10/13 17:23:19 by bhielsch ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
static void ft_inputcheck(int nb, int size, int fd);
static int ft_count(int nb);
void ft_putnbr_fd(int nb, int fd)
{
int size;
size = ft_count(nb);
if (nb == -2147483648)
write(fd, "-2147483648", 11);
else if (nb == 0)
write (fd, "0", 1);
else
ft_inputcheck(nb, size, fd);
}
int ft_count(int nb)
{
int temp;
int count;
temp = nb;
count = 0;
while (temp != 0)
{
temp = temp / 10;
count++;
}
return (count);
}
void ft_inputcheck(int nb, int size, int fd)
{
int x;
char arr[11];
x = 0;
if (nb < 0)
{
nb = nb * -1;
write (fd, "-", 1);
}
if (nb > 0)
{
while (x < size)
{
arr[x] = nb % 10 + '0';
nb = nb / 10;
x++;
}
}
while (x > 0)
{
write (fd, &arr[x - 1], 1);
x--;
}
}