Re: how to read binary file writen by "fwrite"

From: Dave Allured - NOAA Affiliate <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Fri Jan 24 2014 - 15:58:31 MST

feng,

Call cbinread twice.

The first time, just read the value of NumValidCuts by setting dsizes to 1,
and datatype to either integer or int64. You must match the data type of
NumValidCuts to a matching NCL data type. It is probably either integer or
int64, but there is a chance it is something else.

http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclDataTypes.shtml

The second time, read the entire file as a single array of floats. Set
dsizes to -1, see the documentation for cbinread. And set datatype to
"float".

The second result will be one large 1-D array with NumValidCuts, Elevation,
and RData all concatenated together. Use NCL range subscripting to break
this single array into three parts. Range subscripting is described here:

http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml#Subscripts

The first part will be NumValidCuts, but read in the wrong format, so it is
an invalid number. Ignore this part, because you already have the correct
value from the first cbinread.

Also, you must determine whether this part occupies one float position, or
two, by comparing the byte size of NumValidCuts with the byte size of a
standard float. The range subscripting depends on knowing the correct
number of float positions occupied by NumValidCuts.

The second and third parts will be Elevation and RData. Use the onedtond
function to convert this from a 1-D array to a 3-D array.

http://www.ncl.ucar.edu/Document/Functions/Built-in/onedtond.shtml

--Dave

On Fri, Jan 24, 2014 at 1:45 PM, ChenFeng <fengfengabc003@163.com> wrote:

> hi,
>
> Does anybody know how to read binary file writen by "fwrite" in c language.
> The file is writen by the code below:
>
> fpR = fopen(fileR,"wb");
> if(fpR==0) return false;
> fwrite(&NumValidCuts,sizeof(NumValidCuts),1,fpR);
> fwrite(Elevation,sizeof(float),NumValidCuts,fpR);
> fwrite(RData,sizeof(float),NumValidCuts*MaxRads*RGates,fpR);
> fclose(fpR);
>
> I tried fbinrecread, cbinread, but failed.
>
> Thanks.
>
> feng
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Jan 24 15:58:41 2014

This archive was generated by hypermail 2.1.8 : Fri Feb 07 2014 - 16:39:11 MST