Writing an integer to a file with fputs()

前端 未结 4 1383
终归单人心
终归单人心 2021-01-04 13:21

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\", fpt

4条回答
  •  耶瑟儿~
    2021-01-04 13:41

    The provided answers are correct. However, if you're intent on using fputs, then you can convert your number to a string using sprintf first. Something like this:

    #include 
    #include 
    
    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;
    }
    

提交回复
热议问题