binary file format??

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

binary file format??

Rusty Boyd
What is the file format used when using the 'fwrite' function, or, more to the  
point, how can I read a file created with this function using a 'C' program. I  
have tried every precision type listed, but after about 30 or so items from a  
column vector, the numbers start making no sense (to me). For example, instead  
of 0xFFFF, the file will contain FFDF, or, instead of 0x000E, it will be  
0x00E0.
Thanks in advance,
Rusty


Rusty Boyd
OUHEP - http://oua4.nhn.ou.edu/
[hidden email]
office: (405)325-2890 x36316
office fax: (405)325-7557
home ph., fax and data: (405)447-5211
University of Oklahoma
Dept. of Physics and Astronomy
440 W. Brooks
Norman, OK 73019


Reply | Threaded
Open this post in threaded view
|

Re: binary file format??

Kai Mueller-2
On Wed, Jun 10, 1998 at 01:17:51PM -0600, Rusty Boyd wrote:
> What is the file format used when using the 'fwrite' function, or, more to the  
> point, how can I read a file created with this function using a 'C' program. I  
> have tried every precision type listed, but after about 30 or so items from a  
> column vector, the numbers start making no sense (to me). For example, instead  
> of 0xFFFF, the file will contain FFDF, or, instead of 0x000E, it will be  
> 0x00E0.
> Thanks in advance,
> Rusty
>

Octave example:
  a =

    1  2  3
    4  5  6
    7  8  9


  octave:8> fwrite(fn,a,"double")
  ans = 9

Read file in C:

  #include <stdio.h>

  int main()
  {
      FILE  *fp;
      double x;

      if ((fp = fopen("xxx.xxx", "rb")) == NULL)  exit(1);
      for (;;)
      {
          fread(&x, sizeof(double), 1, fp);
          if feof(fp)  break;
          printf("%f\n", x);
      }
      fclose(fp);
      return 0;
  }

This program lists file "xxx.xxx":

  [leo]/XXX$ readbin
  1.000000
  4.000000
  7.000000
  2.000000
  5.000000
  8.000000
  3.000000
  6.000000
  9.000000

(Fortran column-wise dense format).

Kai

--
 Kai P. Mueller
 Control Department (Regelungstechnik) | Phone [+49] (531) 391-3835
 Technical University Braunschweig     | Fax   [+49] (531) 391-5194
 D-38092 Braunschweig                  | Email [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: binary file format??

John W. Eaton-6
On 10-Jun-1998, Kai Mueller <[hidden email]> wrote:

| On Wed, Jun 10, 1998 at 01:17:51PM -0600, Rusty Boyd wrote:
| > What is the file format used when using the 'fwrite' function, or, more to the  
| > point, how can I read a file created with this function using a 'C' program. I  
| > have tried every precision type listed, but after about 30 or so items from a  
| > column vector, the numbers start making no sense (to me). For example, instead  
| > of 0xFFFF, the file will contain FFDF, or, instead of 0x000E, it will be  
| > 0x00E0.
| > Thanks in advance,
| > Rusty
| >
|
| Octave example:
|   a =
|
|     1  2  3
|     4  5  6
|     7  8  9
|
|
|   octave:8> fwrite(fn,a,"double")
|   ans = 9

Also, as I think someone mentioned in another thread, you may need to
open the file in binary mode on non-Unixy systems for this to be
successful.  For example, use

  fn = fopen ("file", "wb");

instead of just

  fn = fopen ("file", "w");

Perhaps this is why you get incorrect results after a number of values
have been written?

jwe