[sclug] C programming question

Andy Arbon sclug at andrewarbon.co.uk
Sat Oct 25 09:05:38 UTC 2003


Hello All,

I'm helping a mate with some C programming, but I'm not exactly a guru 
myself, and I'm a bit puzzled by a problem I'm hitting.

I have the two (hopefully attached) files. primary is a binary datafile, 
showpri.c is code that should read it and display it.

The problem is that when it runs it displays a load of rubbish (couple 
of lines quoted below). The data is supposed to be from a radar, to give 
you some context. This means that negative range readings and enourmous 
bearing angles are unlikely to be correct.

Can anyone tell me what's wrong with it? The datafile was probably 
produced on Windows and I'm running the code on Linux - is there any 
difference in the file I/O between the two that could account for this?

My primitive C skills say that the code is all OK, so I'm wondering if 
it's one of those little-known-quirks type problems that I don't know about.

Cheers,

Andy


No    Bearing   Range   Elevation
                 gates   angle

  0      0.00 -468385792 
-55110200796620348782678121158369662643176976710747638795560799680278459353800660385130122328779655190693409370583986398835662809115595762201875119174608240120998180441773247588543284002479767346363033550840450276533526613437365216370830106013827317366784.00
  1 
-2455544853178971674289851995617191448574354884249111904001649611314200044146899645249402763284588804731320539751053705006499732417900275538546655130161117199547540475922883677417643818432444060842133923317080445211881221602608843331235454762672774893243727872.00 
693931970     0.00
  2 
-4595439111720835063429696069900271515748711667122921690954915206811221122299244731744488821650207505385429718223311723033906026247221561277238617850822122516047132539752890427557340846831506266615089350927552012494376770877053668259825471476203520.00 
1373139520     0.00
  3      0.00 130856089 
738736861112548861520159808165694704305033116502771810580283294354315112429196171915550913685051865729637092309807682764544149377460994048.00
  4      0.00 -2107633344    -0.00

-------------- next part --------------
A non-text attachment was scrubbed...
Name: primary
Type: application/octet-stream
Size: 504 bytes
Desc: not available
Url : http://lists.tmdg.co.uk/pipermail/sclug/attachments/20030514/f87e2705/primary.obj
-------------- next part --------------
/************************************************************
	SHOW PRIMARY DATA

	Author  T G Morgan
	Date    16 Feb 2001
	Issue   1

	This program displays the contents of the file
	'primary' used in the Plot Association assignment.
************************************************************/

#include <stdio.h>

int main()
{
/*	'plots' is an array into which the structures on the
	file are read before being displayed               */

	struct primary_plot {
		double   bearing;
		int      range;
		double   elevation;
	};
	typedef struct primary_plot PLOT;
	PLOT  plots[25];

	int      i=0, n, total;
	FILE    *fp;

/*	Read one structure at a time from the file. fread
	returns the number of structures actually read and
	this value is placed in n. As long as there is still
	data in the file, n will be 1. When the end of the
	file is reached, no further structure will be read,
	so n will be given a value of 0. This terminates the
	loop.                                                */

	fp = fopen("primary", "rb");
	do
	{
		n = fread(&plots[i], sizeof(PLOT), 1, fp);
		i++;
	}
	while (n == 1);

	fclose(fp);
	total = i-1;

/*	The table of plots is now printed to stdout          */
	     
	printf ("\n\nNo    Bearing   Range   Elevation\n");
	printf ("                gates   angle\n"); 

	for (i=0; i<total; i++)
	  printf("\n%2d %9.2lf %7d  %7.2lf",i, plots[i].bearing, plots[i].range ,plots[i].elevation ) ;
	printf ("\n\n"); 
	return 0;
}



More information about the Sclug mailing list