I'm working with some old code and the users tell me that the original program's output was a single binary file containing multiple stuct data. The output I get is a binary file containing a single structure. The comment also indicates that this what is happening but I need to be sure before I tell the user they must have given me the wrong code. I would have thought to output the whole linked list one would need to iterate through said list and output each binary file individually.

The code that writes to the binary file is a single line, as follows:

        /*output RTZ indication set to binary data file*/

        if((fwrite(&(NodePtr->sb),sizeof(struct SbOutput),1,foutp))!=1)
            FatalError(FATAL_FILEIOERR, INFO(errno));

There is no loop, it is contained within a function. It also appears that it overwrites the file everytime the function is called which confuses matters further.

Thanks for any input!

I think you need to show a larger peice of your code and explain your problem a bit more.

Thanks for the response.

I am trying to work out whether or not the following code will produce a binary file containing the entire nodes from the linked list like the user claims. Or will just keep overwriting the same binary file with ONLY ONE node from the list per iteration (not the entire list) as I interpret this.

Here is the function that writes the binary file.

static void  OutputSbRTZSet(FILE * foutp,struct SbListNode * NodePtr)
{
            /*output RTZ indication set to binary data file*/
        if((fwrite(&(NodePtr->sb),sizeof(struct SbOutput),1,foutp))!=1)
         FatalError(FATAL_FILEIOERR, INFO(errno));

}

It is called from the following function only:

    void BeamProc(FILE *foutp, char *path)
    {
          int tnemin;    
          struct SbListNode* NodePtr;
          struct SbListNode* NextNodePtr;

                        /*start at head of list*/
          NodePtr = SbRTZ_Head;
          while (NodePtr != NULL)
          {
                ....
                    OutputSbRTZSet(foutp,NodePtr)                   
                ...
          }
          /* get next */
          NodePtr = NextNodePtr;
        }
    }

Where NodePtr is of SbListNode type:

typedef struct SbListNode
{
       struct SbOutput sb;  //output portion of set node

       // the following bounds are for set association
       int      LastRLoBound;  // last lower range bound
       int      LastRHiBound;  // last upper range bound
       int      TempRHiBound;  // temp upper range bound
       int      LastTLoBound;  // last lower theta bound
       int      LastTHiBound;  // last upper theta bound

       int      AddedFlag;     //peak added to open RT set for this pulse
       int       ThetaExtentNoData; // theta extent of this building RT set
       // in counts since last peak added
       // note that the RT set will close if
       // this extent exceeds TWODEGREES
       int      ThetaExtent;   // total theta extent of RT set
       // note that the RT set will close if
       // ThetaExtent exceeds THIRTYDEGREES
       int       RevsNoPulses;  // count of revs with no data added to set

       //linked list pointers
       struct SbListNode * prev; //ptr to previous node in list
       struct SbListNode * next; //ptr to next node in list
}SB_RTZListToReport;

And sb in SbListNode is:

typedef struct SbOutput
{
       int      index;    //array index where node really lives
       int      mea;     // max echo amplitude counts
       int      mer;     // range counts of max echo amplitude
       int      met;     // theta counts of max echo amplitude
       unsigned  mez;           // axial counts of max echo amplitude
       int      tne;     // total number of echos
       // this is the count of pulses which had
       // at least 1 peak for the RT set
       int      rmin;     // range min of set counts
       int      rmax;     // range max of set counts
       int       tmin;          // theta min of set counts
       int       tmax;          // theta max of set counts
       unsigned  zmin;          // axial min of set counts
       unsigned  zmax;          // axial max of set counts
       long mpulsenum;     //pulse number containing max echo amp
}Output;
if((fwrite(&(NodePtr->sb),sizeof(struct SbOutput),1,foutp))!=1)

Well, the above fwrite() call would write only single instance of struct SbOutput to the file. The thing to take care is that the file pointer foutp should remain consistent ie it should not be updated in between the calls to OutputSbRTZSet() until all the struct SbOutput instances are written to file. Otherwise it may overwrite the exisiting data in file (which was written with fwrite() earlier).

Also make sure that NodePtr is updated with NodePtr->next everytime a call to OutputSbRTZSet is made.

commented: Thanks, this resolved the issue! The pointer was being updated. Thanks for the detailed explanation it is much appreciated. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.