Thread: reading structured arrays, changing their structure

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    reading structured arrays, changing their structure

    hi,

    i am trying to read in two different structured arrays, place them in a different structure so they are both the same and then compare them.

    the first array, 'primary', has the structure..

    Code:
     struct primary_plot {
      double   bearing; /*degrees */
      int      range;  /*metres */
      double   elevation; /*degrees */
     };
    the second array, 'secondary', has a similar structure..

    Code:
     struct secondary_plot {
      double bearing; /*degrees */
      int range;  /*metres */
      int altitude; /*metres */
      int flight_number;
     };
    i need to read these arrays into a file, and store them in seperate arrays with this structure..

    Code:
    struct aircraft_information {
      double bearing; /*degrees */
      double  range;  /*kilometres */
      int altitude; /*metres */
      int flight_number;
      int  match;  /*array index of matching plot */
    obviously i will need to convert the range's from both the primary and secondary into kilometres which isnt a problem, im just unsure of how to carry out this task.

    any help will be appreciated!

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Given your information alone, it's difficult to determine an answer. Maybe multiply the two values and divide by 1000?

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    3
    sorry, i should of explained better.

    i have the formula to convert metres into kilometres, i was unsure about how to read the arrays in and put them into the different structure shown.

    thanks again

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Did you create the file you are going to be reading from, or was it already created for you? If so, is it a random access, or sequential file? Depending on which type, you could use fwrite() or fgets() to get that information. Then break it up and assign each struct member its values.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    3
    i created the file myself, using the following code..

    Code:
    #include <stdio.h>
    
    int main()
    {
    
            struct primary_plot {
       double bearing;
       int  range;
       double elevation;
            };
            typedef struct primary_plot   PLOT;
            PLOT    plots[25];
    
            struct primary_location {
       double bearing;
       double km_range;
       double altitude;
            };
            typedef  struct primary_location   LOCATION;
            LOCATION loc[25];
    
            int       i;
      FILE *fp;
    
            loc[0].km_range = 140.1 ;
            loc[0].bearing  = 260.17 ;
            loc[0].altitude = 12063 ;
    
            loc[1].km_range = 20.3;
            loc[1].bearing  = 281.31;
            loc[1].altitude = 4312;
    
            loc[2].km_range = 20.10;
            loc[2].bearing  = 301.52;
            loc[2].altitude = 3310;
    
            loc[3].km_range = 120.35;
            loc[3].bearing  = 301.51;
            loc[3].altitude = 10078;
    
            loc[4].km_range = 97.15;
            loc[4].bearing  = 328.29;
            loc[4].altitude = 7320;
    
            loc[5].km_range = 170.12;
            loc[5].bearing  = 353.20;
            loc[5].altitude = 6340;
    
            loc[6].km_range = 273.16;
            loc[6].bearing  = 349.17;
            loc[6].altitude = 12750;
    
            loc[7].km_range = 52.73;
            loc[7].bearing  = 0.02;
            loc[7].altitude = 2160;
    
            loc[8].km_range = 81.18;
            loc[8].bearing  = 28.31;
            loc[8].altitude = 3720;
    
            loc[9].km_range = 221.31;
            loc[9].bearing  = 42.11;
            loc[9].altitude = 6815;
    
            loc[10].km_range = 42.31;
            loc[10].bearing  = 83.97;
            loc[10].altitude = 2170;
    
            loc[11].km_range = 42.47;
            loc[11].bearing  = 84.11;
            loc[11].altitude = 2130;
    
            loc[12].km_range = 16.15;
            loc[12].bearing  = 86.10;
            loc[12].altitude = 753;
    
            loc[13].km_range = 73.42;
            loc[13].bearing  = 103.97;
            loc[13].altitude = 7300;
    
            loc[14].km_range = 73.15;
            loc[14].bearing  = 102.73;
            loc[14].altitude = 8020;
    
            loc[15].km_range = 259.63;
            loc[15].bearing  = 151.96;
            loc[15].altitude = 12630;
    
            loc[16].km_range = 278.10;
            loc[16].bearing  = 146.39;
            loc[16].altitude = 13150;
    
            loc[17].km_range = 103.15;
            loc[17].bearing  = 198.21;
            loc[17].altitude = 8420;
    
            loc[18].km_range = 45.18;
            loc[18].bearing  = 207.52;
            loc[18].altitude = 5190;
    
            loc[19].km_range = 310.15;
            loc[19].bearing  = 248.68;
            loc[19].altitude = 13150;
    
            loc[20].km_range = 10.28;
            loc[20].bearing  = 252.13;
            loc[20].altitude = 853;
    
      for (i=0; i<21; i++)
      {
       plots[i].bearing   = loc[i].bearing;
       plots[i].range     = (int) (loc[i].km_range * 40.0);
       plots[i].elevation = (loc[i].altitude/(loc[i].km_range*1000) - loc[i].km_range/15200)*180/3.14159;
      };
    
      fp = fopen("primary", "wb");
      fwrite(&plots, sizeof(PLOT), 21, fp);
      fclose(fp);
      return 0;
    }
    how would i go about using fgets() in my situation?

    thanks again for the reply.

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Here, i have edited your code in some places, you will notice, and i have added a fuction to read the file. There are some bad practices in here, but you can use this to get an idea.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void blah(void);
    
    
    struct primary_plot {
    	double bearing;
    	double  range;
    	double elevation;
    };
    typedef struct primary_plot   PLOT;
    
    
    struct primary_location {
    	double bearing;
    	double km_range;
    	double altitude;
    };
    typedef  struct primary_location   LOCATION;
    
    
    
    int main()
    {
    	
    	
    	int       i;
    	FILE *fp;
    	LOCATION loc[25];
    	PLOT    plots[25];
    	
    	loc[0].km_range = 140.1 ;
    	loc[0].bearing  = 260.17 ;
    	loc[0].altitude = 12063 ;
    	
    	loc[1].km_range = 20.3;
    	loc[1].bearing  = 281.31;
    	loc[1].altitude = 4312;
    	
    	loc[2].km_range = 20.10;
    	loc[2].bearing  = 301.52;
    	loc[2].altitude = 3310;
    	
    	loc[3].km_range = 120.35;
    	loc[3].bearing  = 301.51;
    	loc[3].altitude = 10078;
    	
    	loc[4].km_range = 97.15;
    	loc[4].bearing  = 328.29;
    	loc[4].altitude = 7320;
    	
    	loc[5].km_range = 170.12;
    	loc[5].bearing  = 353.20;
    	loc[5].altitude = 6340;
    	
    	loc[6].km_range = 273.16;
    	loc[6].bearing  = 349.17;
    	loc[6].altitude = 12750;
    	
    	loc[7].km_range = 52.73;
    	loc[7].bearing  = 0.02;
    	loc[7].altitude = 2160;
    	
    	loc[8].km_range = 81.18;
    	loc[8].bearing  = 28.31;
    	loc[8].altitude = 3720;
    	
    	loc[9].km_range = 221.31;
    	loc[9].bearing  = 42.11;
    	loc[9].altitude = 6815;
    	
    	loc[10].km_range = 42.31;
    	loc[10].bearing  = 83.97;
    	loc[10].altitude = 2170;
    	
    	loc[11].km_range = 42.47;
    	loc[11].bearing  = 84.11;
    	loc[11].altitude = 2130;
    	
    	loc[12].km_range = 16.15;
    	loc[12].bearing  = 86.10;
    	loc[12].altitude = 753;
    	
    	loc[13].km_range = 73.42;
    	loc[13].bearing  = 103.97;
    	loc[13].altitude = 7300;
    	
    	loc[14].km_range = 73.15;
    	loc[14].bearing  = 102.73;
    	loc[14].altitude = 8020;
    	
    	loc[15].km_range = 259.63;
    	loc[15].bearing  = 151.96;
    	loc[15].altitude = 12630;
    	
    	loc[16].km_range = 278.10;
    	loc[16].bearing  = 146.39;
    	loc[16].altitude = 13150;
    	
    	loc[17].km_range = 103.15;
    	loc[17].bearing  = 198.21;
    	loc[17].altitude = 8420;
    	
    	loc[18].km_range = 45.18;
    	loc[18].bearing  = 207.52;
    	loc[18].altitude = 5190;
    	
    	loc[19].km_range = 310.15;
    	loc[19].bearing  = 248.68;
    	loc[19].altitude = 13150;
    	
    	loc[20].km_range = 10.28;
    	loc[20].bearing  = 252.13;
    	loc[20].altitude = 853;
    	
    	for (i=0; i<21; i++)
    	{
    		plots[i].bearing   = loc[i].bearing;
    		plots[i].range     = (loc[i].km_range * 40.0);
    		plots[i].elevation = (loc[i].altitude/(loc[i].km_range*1000) - loc[i].km_range/15200)*180/3.14159;
    	};
    	
    	fp = fopen("primary", "wb");
    	if (fp == NULL)
    	{
    		printf("Blah. Error.");
    		exit(0);
    	};
    	fwrite(&plots, sizeof(PLOT), 21, fp);
    	fclose(fp);
    	blah();
    	return 0;
    }
    
    void blah(void)
    {
    	FILE *fp;
    	int i;
    	PLOT tempstruct[25];
    	
    	fp = fopen("primary", "r");
    	if (fp == NULL)
    	{
    		printf("Blah Error again!!!");
    		exit(0);
    	};
    	
    	for (i=0; i<20; i++)
    	{
    		fread(&tempstruct[i], sizeof(PLOT), 1, fp);
    		printf("%lf  %lf  %lf\n", tempstruct[i].range,
    					  tempstruct[i].bearing,
    					  tempstruct[i].elevation);
    	};
    	fclose(fp);
    }
    Last edited by stumon; 05-15-2003 at 08:32 AM.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    loc[0].km_range = 140.1 ;
    loc[0].bearing  = 260.17 ;
    loc[0].altitude = 12063 ;
    Ack. What a nightmare. You should just initialize your arrays when you declare them:
    Code:
    struct foo bar[SIZE] =
    {
        { 1.2, 3.4, 5.6 },
        { 7.8, 9.0, 1.2 },
         ... lather, rinse, repeat...
        { 3.4, 5.6, 7.8 }
    };
    So much cleaner.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 09:20 AM
  2. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  3. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 12:35 PM
  4. reading a string into a structure
    By breed in forum C Programming
    Replies: 1
    Last Post: 01-16-2002, 03:34 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM