'trying to write integers into file genreates random characters in C language

I'm trying to make a random input.txt file generator in C language but I keep getting random characters in the output file. I tried using both putw() and fwrite() but I get the same result.

    for(int i = 0; i < 100; i++){
        ran_numb = rand()%(ram_size-1);
        // printf("%d", ran_numb);
        // int err = putw(ran_numb, fp);
        fwrite(&ran_numb, sizeof(int), 1, fp);
        fputc('\n', fp);
    }

output file:

~   
ô  
  
u   
?  
  
  
   
{  
ø  
  
È   
Õ   
å  
    
×  
¡  

full code:

#include <stdio.h>
#include <string.h>

int main (int argc, char* argv){
    generate_input_file(2048);

}

void generate_input_file(int ram_size){
    int ran_numb = 0;
    FILE *fp = fopen("input.txt", "w");;

    if(fp == NULL){
        printf("can't open file!");
    }

    for(int i = 0; i < 100; i++){
        ran_numb = rand()%(ram_size-1);
        // printf("%d", ran_numb);
        // int err = putw(ran_numb, fp);
        fwrite(&ran_numb, sizeof(int), 1, fp);
        fputc('\n', fp);
    }


}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source