Thread: Problems in reading binary file

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Problems in reading binary file

    I have difficulty in reading the content of the binary file. When I tried to open the .bin file, all I could see were the weird letters as below:

    Code:
    ︡㭵喾㭑王㫯笯㬱읓㬹뙷㫮潶㭂ᯠ㬓몑㭣膛㮱㮅孛㬺ݺ㲢㶊국㵗龫㶊빈㶩⮵㵯披㳸鱊㳉酸㲮㱷ꌿ㯻되㬩㻬㭕⼦㬲좰㬬卞㭄옿㭊韝㭗庺㮓緕㯭묚㰌䟛㵼ዹ㶏混㶙⌅㶏첩㳮呂㲯眶㴲뀈㰔Წ㰣댋㯫偫㮜헒㮕獎㮋菁㮃糟㮐쬫㮔灛㮩㰍⻳㰩ꓜ㱩ㇳ㴫萻㶨휙㶩廱㷅⩼㶃迨㴙㴑廀㲮ፑ㳾׊㱃傇㰋ﺃ㯟㯶먘㯁ꅹ㯐⮧㯽㰓л㱂ꘓ㱺跍㲥વ㴎㫶㶍㷹鍘㸚襉㷗跭㶄傱㵢㯹樌㱙ᅬ㱔⁹㰘䁰㰑䪮㰏崌㰅㰍ૅ㰍⿂㰌랑㱐砫㲇�㲡᝛㳲윁㶞㷨⶧㸸颡㸅ꏽ㶻ﺈ㷒ꂳ㯒㰨๣㱠抦㰿㱁㰸㰨㰨應㰤脲㱂秞㱛ԉ㲉㲤ྃ㳔稊㵑⨍㷚儧㸶찅㸜訳㷘ꇷ㸕�㰪㲨䒕㲊鯇㱎ே㱙鱑㱗蟺㱀㰽뒩㱕뵩㱭̫㲂㖥㲏媖㲦辻㲸鮅㴪붋㷄㸄윱㸚뼌㷏戣㷥禭㴁袅㳓㕿㱙韹㱖♮㱪痭㱙遙㱢纚㱢蜮㱫ꈛ㱧�㲂뾛㲉疃㲠௉㲬ࠆ㴌먹㶭俦㷦栆㸏�㷤昬㸳䵸㲻䰠㴟娍㲆㽖㱳쨉㱡각㲁�㱵쨉㱡祀㱜চ㱮树㱬㱡躒㱲쬕㲬겎㳔幦㶇᠞㶶뫻㷸
    Is there any way that I can convert them to readable letters?I suppose the contents are in numerical floating point form. Here is the program that generate the binary file. I just need to see the contents of the output files (T11.bin,T12_real.bin,T12_imag.bin,T13_real.bin,T1 3_imag.bin,T22.bin,T23_real.bin, T23_imag.bin,T33.bin).



    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    
    #ifdef _WIN32
    #include <dos.h>
    #include <conio.h>
    #endif
    
    
    /* ALIASES  */
    
    
    /* T matrix */
    #define T11     0
    #define T12_re  1
    #define T12_im  2
    #define T13_re  3
    #define T13_im  4
    #define T22     5
    #define T23_re  6
    #define T23_im  7
    #define T33     8
    
    
    /* CONSTANTS  */
    
    #define Npolar_in   9		/* nb of input/output files */
    #define Npolar_out  9
    
    
    /* ROUTINES DECLARATION */
    #include "matrix.h"
    #include "util.h"
    
    
    int main(int argc, char *argv[])
    {
    
    
    /* LOCAL VARIABLES */
    
    
    /* Input/Output file pointer arrays */
        FILE *in_file, *out_file[Npolar_out];
    
    /* Strings */
        char toto[20];
        char *buf;
        char file_name[128], out_dir[128];
        char *file_name_out[Npolar_out] = { "T11.bin", "T12_real.bin","T12_imag.bin",
                                            "T13_real.bin","T13_imag.bin",	"T22.bin",
                                            "T23_real.bin", "T23_imag.bin",	"T33.bin" };
        char PolarCase[20], PolarType[20];
    
    /* Input variables */
        int Ncol;			/* Initial image nb of lines and rows */
        int Off_lig, Off_col;	/* Lines and rows offset values */
        int M_Nlig, M_Ncol;	/* Sub-image nb of lines and rows */
        int Header;			/* Flag for Header 0=No 1=Yes */
        int SubSampRG, SubSampAZ;
    
    /* Internal variables */
        int np, i, j, k;
        
        int b[10];
        long unsigned int kl, reclength;
    
        float p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
        float m_a0pb0, m_a0pb, m_a0mb, m_ma0pb0;
        float m_c, m_d, m_e, m_f, m_g, m_h;
    
    
    /* Matrix arrays */
        float **M_in;		/* Kennaugh matrix 3D array (lig,col,element) */
        float **M_out;		/* T matrix 2D array (col,element) */
    
    
    /* PROGRAM START */
    
        if (argc == 11) {
    	strcpy(file_name, argv[1]);
    	strcpy(out_dir, argv[2]);
    	Ncol = atoi(argv[3]);
    	Off_lig = atoi(argv[4]);
    	Off_col = atoi(argv[5]);
    	M_Nlig = atoi(argv[6]);
    	M_Ncol = atoi(argv[7]);
    	Header = atoi(argv[8]);
    	SubSampRG = atoi(argv[9]);
    	SubSampAZ = atoi(argv[10]);
        } else
    	edit_error
    	    ("airsar_convert_T3 stk_file_name out_dir Ncol offset_lig offset_col sub_nlig sub_ncol Header (0/1) SubSampRG SubSampAZ","");
    
        check_file(file_name);
        check_dir(out_dir);
    
    /* Nb of lines and rows sub-sampled image */
        M_Nlig = (int) floor(M_Nlig / SubSampAZ);
        M_Ncol = (int) floor(M_Ncol / SubSampRG);
        strcpy(PolarCase, "monostatic");
        strcpy(PolarType, "full");
        write_config(out_dir, M_Nlig, M_Ncol, PolarCase, PolarType);
    
        M_in = matrix_float(Npolar_in, Ncol);
        M_out = matrix_float(Npolar_out, M_Ncol);
        buf = vector_char(Ncol * 10);
    
    /* INPUT/OUTPUT FILE OPENING*/
    
        if ((in_file = fopen(file_name, "rb")) == NULL)
    	edit_error("Could not open input file : ", file_name);
    
        for (np = 0; np < Npolar_out; np++) {
    	sprintf(file_name, "%s%s", out_dir, file_name_out[np]);
    	if ((out_file[np] = fopen(file_name, "wb")) == NULL)
    	    edit_error("Could not open output file : ", file_name);
        }
    
    /* SKIP HEADER IF EXISTS */
        if (Header == 1) {
    	rewind(in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	fgets(buf, 51, in_file);
    	strncpy(toto, &buf[40], 10);
    	toto[10] = '\0';
    	reclength = atol(toto);
    	rewind(in_file);
    	for (kl = 0; kl < reclength; kl++)
    	    fgets(buf, 2, in_file);
        }
    
    /* OFFSET LINES READING */
        for (i = 0; i < Off_lig; i++)
    	fread(&buf[0], sizeof(char), 10 * Ncol, in_file);
    
    
    /* READING AND MULTILOOKING */
        for (i = 0; i < M_Nlig; i++) {
    	if (i%(int)(M_Nlig/20) == 0) {printf("%f\r", 100. * i / (M_Nlig - 1));fflush(stdout);}
    
        fread(&buf[0], sizeof(char), 10 * Ncol, in_file);
    
        for (j = 0; j < Ncol; j++) {
    		for (k = 0; k < 10; k++)
    		    b[k] = (signed int) buf[10 * j + k];
    
    		p1 = (1.5 + (float) b[1] / 254.) * pow(2., (float) b[0]);
    		p2 = (float) b[2] / 127.;
    		p3 = (float) b[3] * b[3] / 16129.;
    		if (b[3] < 0)
    		    p3 = -p3;
    		p4 = (float) b[4] * b[4] / 16129.;
    		if (b[4] < 0)
    		    p4 = -p4;
    		p5 = (float) b[5] * b[5] / 16129.;
    		if (b[5] < 0)
    		    p5 = -p5;
    		p6 = (float) b[6] * b[6] / 16129.;
    		if (b[6] < 0)
    		    p6 = -p6;
    		p7 = (float) b[7] / 127.;
    		p8 = (float) b[8] / 127.;
    		p9 = (float) b[9] / 127.;
    		p10 = 1. - p7 - p9;
    
    		m_a0pb0 = 2. * p1;
    		m_a0pb = 2. * p1 * p10;
    		m_a0mb = 2. * p1 * p7;
    		m_ma0pb0 = 2. * p1 * p9;
    		m_c = 2. * p1 * p2;
    		m_d = -2. * p1 * p8;
    		m_e = 2. * p1 * p5;
    		m_f = -2. * p1 * p4;
    		m_g = -2. * p1 * p6;
    		m_h = 2. * p1 * p3;
    
    		M_in[T11][j] = (m_a0pb0 + m_a0pb + m_a0mb - m_ma0pb0) / 2.;
    		M_in[T22][j] = (m_a0pb0 + m_ma0pb0 + m_a0pb - m_a0mb) / 2.;
    		M_in[T33][j] = (m_a0pb0 + m_ma0pb0 - m_a0pb + m_a0mb) / 2.;
    		M_in[T12_re][j] = m_c;
    		M_in[T12_im][j] = -m_d;
    		M_in[T13_re][j] = m_h;
    		M_in[T13_im][j] = m_g;
    		M_in[T23_re][j] = m_e;
    		M_in[T23_im][j] = m_f;
    	    }
    
    	for (j = 0; j < M_Ncol; j++) {
    	    for (np = 0; np < Npolar_out; np++)
    		M_out[np][j] = M_in[np][Off_col + j * SubSampRG];
            }			/*j */
    
    	for (np = 0; np < Npolar_out; np++)
    	    fwrite(&M_out[np][0], sizeof(float), M_Ncol, out_file[np]);
               
    	for (j = 1; j < SubSampAZ; j++)
             fread(&buf[0], sizeof(char), 10 * Ncol, in_file);
    
        }				/*i */
    
    
    /* FILE CLOSING */
        fclose(in_file);
        for (np = 0; np < Npolar_out; np++)
    	fclose(out_file[np]);
    
        free_matrix_float(M_out, Npolar_out);
        free_matrix_float(M_in, Npolar_in);
    
        return 1;
    }			/*main */
    I'm looking for a favourable reply from you. Thank you very much.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you need the contents of a file to be human readable, use text instead of binary.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fgets(buf, 51, in_file);
    Using fgets() on a file opened in "rb" mode as well is generally a bad idea.

    Also, split some of the work main() is doing into other functions.
    Above a couple of pages in length, code harder to follow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    I'm looking for a favourable reply from you
    it's a good job you said that ... coz... i was about to... well.. ya know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-27-2009, 05:56 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM