Error check problem again
Here, I'll start over with the code. My problem is is that I keep on getting a SUN_DATA_ERR returned even though a good text file doesn't constitute the SUN_DATA_ERR.
Code:
int Find_Habitable_Planets (
char *Filename_In,
char *Filename_Out,
Coord_Type Pos,
float Range)
{
/* find a number of habitable planets in range and output this information
* to a file in alphabetical order according to planet name */
FILE *Read_FP = NULL;
FILE *Write_FP = NULL;
System_Type System;
int Count_System = 0; /* Variable for scanning system. */
/* int Count_Planet = 0;*/ /* Variable for scanning planet. */
/* int I = 0;*/ /* Planet Increment loop */
Read_FP = fopen (Filename_In, "r");
if (Read_FP == NULL)
{
return (FILE_READ_ERR);
}
Write_FP = fopen (Filename_Out, "w+");
if (Write_FP == NULL)
{
fclose (Read_FP);
Read_FP = NULL;
return (FILE_WRITE_ERR);
}
while (Count_System != 2)
{
Count_System = fread (&System, sizeof (System_Type), 1, Read_FP);
printf("\n%i %i", Count_System, feof (Read_FP));
if ( (Count_System != 1 )
&& (Count_System != EOF))
{
clearerr (Read_FP);
fclose (Read_FP);
Read_FP = NULL;
fclose (Write_FP);
Write_FP = NULL;
return (SUN_DATA_ERR);
}
if (Count_System == EOF)
{
Count_System = 2;
}
}
return (BAD_STAR_CLASS);
} /* Find_Habitable_System */
Sorry but can't reply to the other thread
Here's the header file:
Code:
#include <stdlib.h>
/* Constant Definitions */
#define MAXFILELEN ( 30 ) /* max file name length */
#define MAX_NAME_LEN ( 32 ) /* max name length */
#define MAX_PLANETS ( 10 ) /* max number of planets in any system */
#define MAX_SUNS ( 4 ) /* max number of suns in any system */
/* Error Codes */
#define BAD_RECORD ( -3 ) /* unexpected fields/chars in record */
#define FILE_READ_ERR ( -4 ) /* can't open file for read */
#define FILE_WRITE_ERR ( -5 ) /* can't open file for write */
#define SUN_DATA_ERR ( -8 ) /* star data invalid */
#define PLANET_DATA_ERR ( -9 ) /* planet data invalid */
#define BAD_SUBCLASS ( -11) /* star subclass invalid */
#define BAD_STAR_CLASS ( -12) /* star class invalid */
#define NOT_HABITABLE ( -22) /* no planet in system is habitable */
#define INVALID_WARP ( -32) /* invalid warp factor number */
/* Warp codes and constants */
#define OK_WARP ( 0 ) /* valid warp factor number */
#define MIN_WARP ( 1.0) /* minimum warp factor number */
#define MAX_WARP (9.999) /* maximum warp factor number */
#define CRONS_FACTOR (3.323) /* cochrane's factor for determine speed */
#define WARP_FAC_MIN ( 3.0 ) /* typical minimum warp factor number */
#define WARP_FAC_MAX ( 7.1 ) /* typical maximum warp factor number */
/* Other Codes */
#define NONE ( 0 ) /* no items */
#define OK ( 0 ) /* no errors, everything as should be */
#define TRUE ( 1 )
#define MAX_CLASS_LIST ( 30 ) /* max string length to specify classes */
#define MAX_LEN_LIST ( 15 ) /* max length of list for systems */
/* Planet (habitable) info */
#define MIN_MASS ( 0.5) /* minimum mass necessary for planet */
#define MAX_MASS ( 1.2) /* maximum mass necessary for planet */
#define MIN_O2 (0.13) /* minimum oxygen necessary for planet */
#define MAX_O2 (0.31) /* maximum oxygen necessary for planet */
#define MIN_TEMP ( 270) /* minimum temperature necessary for planet */
#define MAX_TEMP ( 301) /* maximum temperature necessary for planet */
#define BOLZ_CONST (0.049655) /* constant for determining temperature */
#define YEAR_2_DAY (365.25) /* conversion from year to # of days */
/* useful macros */
#define SQR(x) ((x) * (x))
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#define access(x,y) (printf("\n\n\n\n\nDo not use access().\n"), abort(), 1)
/* structure definition */
struct Coord_Struct {
float X;
float Y;
float Z;
};
typedef struct Coord_Struct Coord_Type;
struct Planet_Struct {
char Name[MAX_NAME_LEN];
float Mass;
float Dist;
float Oxygen;
int Num_Moons;
};
typedef struct Planet_Struct Planet_Type;
struct System_Struct {
char Name[MAX_NAME_LEN];
char Class[MAX_CLASS_LIST];
float Magnitude;
Coord_Type Pos;
int Num_Planets;
Planet_Type Planet[MAX_PLANETS];
};
typedef struct System_Struct System_Type;
/* star look-up table */
#define STAR_CLASS_NUM ( 8 )
#define NUM_SUBCLASSES (10 )
/* Function Prototypes */
int Habitable_Planet_Test(Planet_Type *, int, int);
int Time_Of_Travel(float, float, float *);
int Find_Habitable_Planets(char *, char *, Coord_Type, float);