Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an integer to a file with fputs()

Tags:

c

file

integer

It's not possible to do something like fputs(4, fptOut); because fputs doesn't like integers. How can I work around this?

Doing fputs("4", fptOut); is not an option because I'm working with a counter value.

like image 838
Pieter Avatar asked Feb 09 '10 13:02

Pieter


1 Answers

I know 6 years too late but if you really wanted to use fputs

char buf[12], *p = buf + 11;
*p = 0;
for (; n; n /= 10)
    *--p = n % 10 + '0';
fputs(p, fptOut);

Should also note this is for educational purpose, you should stick with fprintf.

like image 106
rosghub Avatar answered Sep 24 '22 17:09

rosghub