How to copy binary integer into a file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LiseCosta
    New Member
    • Apr 2020
    • 1

    #1

    How to copy binary integer into a file?

    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <stdint.h>
    #include <stdlib.h>
    //#define BUFFER_SIZE 1024
    
    int main(int argc, char *argv[]){
    	char file_name[]="out.txt";
    	int v;
    	
    		
    	FILE* f=fopen(file_name,"wb");
    		if(f==NULL){
    			printf("Erro ao abrir ficheiro\n");
    			return 2;
    		}
    		else 
    		
    			
    		while(scanf("%d",&v)==1){
    			fwrite(&v,sizeof(v),4,f);
    		}
    		
    		fclose(f);
    			
    return 0;
    It compiles fine but when I try to open the file it says:the file does not look like a text file or the file encoding is not supported.
    Last edited by gits; Apr 21 '20, 06:54 AM. Reason: added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    Mention the platform, environment, compiler, etc. details.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Do you get that error message when your program runs or later when you try to look at the file after your program has ended?

      It shouldn’t matter, but try changing the filename to “out.bin”.

      Comment

      Working...