Binary File Dispaly Content

HI! I want to write integers from 0 to 99 to the binary file!!! I have this code.....BUt WHEN i display it ( od -vtu1 file.bin), there are bunch of numbers which are not i need. I need only numbers from 0 to 99. This is my code. Help me please. Thank you.
#include <stdio.h>

int main (){
int i;
FILE *file = fopen("file.bin", "wb");
for (i=0;i<100;i++){
if(file!=NULL){
fwrite(&i,sizeof(i),99,file);
/*printf("Value is:%d\n", i);*/
}
fclose(file);
}




return 0;
}

Comments

  • Study this:
    #include <stdio.h>
    
    int main (){
       int i;
       FILE *file = fopen("file.bin", "wb");
       if(!file){
          printf("Error opening file\n");
          return 0;
       }
    
       for (i='A';i<='z';i++){
          fwrite(&i,sizeof(char),1,stdout);
          /*printf("Value is:%d\n", i);*/
       }
       fclose(file);
       printf("\n");
    
       return 0;
    }
    
  • Gud wrk pal
  • >HI! I want to write integers from 0 to 99 to the binary file!!!

    >for (i='A';i<='z';i++){

    Those do not produce the same results.

    >BUt WHEN i display it ( od -vtu1 file.bin), there are bunch of numbers which are not i need

    Because most binary files are unreadable by humans. If you write the binary value of an integer you are writing exactly how it is represented in your computer's memory. You can't use a normal text editor such as Notepad.exe to read the file.
Sign In or Register to comment.