Writing an integer to a file using fputs ()

It is impossible to do something like this fputs(4, fptOut);because fputs does not like integers. How can I get around this?

Execution is fputs("4", fptOut);not an option because I work with a counter value.

+5
source share
4 answers

What about

fprintf(fptOut, "%d", yourCounter); // yourCounter of type int in this case

The documentation fprintfcan be found here .

+12
source
fprintf(fptOut, "%d", counter); 
+4
source

. , fputs, , sprintf. - :

#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv){  
  uint32_t counter = 4;
  char buffer[16] = {0}; 
  FILE * fptOut = 0;

  /* ... code to open your file goes here ... */

  sprintf(buffer, "%d", counter);
  fputs(buffer, fptOut);

  return 0;
}
+4

6 , fputs

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

, , fprintf.

+1

All Articles