Error check problem again

This is a discussion on Error check problem again within the C Programming forums, part of the General Programming Boards category; Here, I'll start over with the code. My problem is is that I keep on getting a SUN_DATA_ERR returned even ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    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 */

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Follow the logic of the function.

    >>if ((Count_System != 1) && (Count_System != EOF))
    So, the first time through this code, Count_System will be 0, making both tests TRUE. You then go on to close the file pointers and return SUN_DATA_ERR.

    You need better logic to control the read loop.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    I'm using fread() because I don't know how to pass an argument of a Structure Type in the control string of the fscanf function. (like %i is for integer, or %s is for character array)
    Can somebody help me with this, since this fread doesn't seem to work?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Based on my previous post.... I missed this:
    >>Count_System = fread(&System, sizeof(System_Type), 1, Read_FP);

    If you have an array of structs to receive multiple data items, why not do something like this (not tested):

    Code:
    int i = 0;
    while (fread(&System[i], sizeof(System_Type), 1, Read_FP) == 1)
      i++;
    You must do binary IO, as Salem said, for both the read and the writes.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    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);

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    Here's the data file:

    Code:
    RVKNSU,A5III,18.496075,-52.619370,92.401382,-94.293282,5
    QWN ^FzE{fVF,0.943865,0.469209,0.027082,0
    TXEgYbuD>h~|2,0.427222,2.270688,0.082083,0
    %dIww.puuN,2.085704,18.947239,0.018158,0
    #xqNrM9`MZ! 2vemmaVB,128.680054,37.861099,0.005619,14
    <$fn>p|ezI,5.877151,53.240952,0.005457,0
    loCyg,E3.5IV,19.467777,-13.361332,-11.753412,11.853407,4
    v@zM<E:iUBe0D0LG11,6.638057,3.051301,0.003451,0
    UC0KsD9&$%.oCNL P^B,2.585788,9.214735,0.016165,0
    ~7IxRxY]SjH/Nb4u{CBn8,258.170563,9.504887,0.011612,0
    /{ 9(^Cro:H59,255.905136,11.633005,0.007666,6
    @`P V,E0.5Ia+A7.5IV+E7Ib,3.662317,186.556671,19.429029,-5.561722,3
    {SLI7INbq#5f]}ix#Fn0,20.735292,4.392074,0.013768,1
    >7Q.S{6]w WYNY<Bo,223.871841,31.919449,0.011230,18
    $n3I1JQokx$TdzE3w2V!lcC!wK0O,41.104950,51.762871,0.006154,7
    Pmj@&Y,G5II+O3Ib+E5.5Ia,19.149792,-64.742760,164.601776,-29.004549,4
    (44uz6bD%@X[,9.286383,10.465923,0.040907,0
    (r!iS 4>T/VMh,59.189514,40.809769,0.010478,8
    ~aDt&q;#AJam|,349.461792,71.881790,0.013091,64
    (7QXlveYv8:acg`eKt]T];@G,2.290522,124.851463,0.004445,0
    }*iGy,B7.5Ib+A4II+F3III+O3III,13.496325,-70.602470,-175.653214,74.438278,3
    XUmW %[Y$QZb4,28.616579,51.103588,0.006424,4
    cKVne;@Uyy| kKMp Mf#[@EO$},61.086746,62.198872,0.009421,4
    /C<)IT5#;$(2&lhhlM,9.338560,91.099998,0.012315,0
    c&l]Z,G1.5Ia+M7Ia+O4.5IV+K4.5Ia,-0.549972,161.329941,-164.581772,34.256287,2
    ojyz6]cw$KpW2R1KZR Q|PTG`SG3,243.267685,66.078201,0.002303,21
    K&tH@Qox6:*iGv8(oj34Vd3gCrZ,55.730461,109.803909,0.007396,3
    l]uQ#,O0.5Ib+G0.5III+F1IV,-4.831509,-25.036749,-55.883205,-62.034897,4
    [$u4(~X6j.4.1V1.1i1>ZtZ.<g,14.502549,20.852436,0.003733,1
    2G5rp3<>6NLMh{`>U I,131.171463,53.472134,0.019828,12
    u}k!R2wmb[w,220.891266,96.971336,0.007629,37
    LvOLoNk2OT% `;qlW,8.673367,136.561264,0.002569,0
    )rJ}1z///ashp,W3Ia+O2.5II+F0.5Ia+M6.5Ia,0.106745,78.518074,146.506668,112.264389,0
    /|n$@T,G2Ia+K7.5III,18.279837,-121.415932,136.255188,-195.288239,2
    b][`X/8[iDLVp,10.024737,2.763927,0.063810,1
    @#hHqP>$Yn.O1<DP>}lI<u,8.102182,15.573329,0.011306,0
    nh.`<T,K3.5Ib+A8IV,3.046598,157.522125,-136.311188,-33.100346,2
    83#F(4lPTV^;}e}SK8%W^BPV,153.918884,23.142357,0.006480,4
    HJGa~uH6)Tpvh``|,2.625596,58.666599,0.006988,0
    ncqoM,E7III+E1.5IV+M7.5IV+K4.5III,5.360482,-167.637619,130.059494,62.326885,2
    Vq] 7B^Z6FpEoue[Ml,149.915878,15.799920,0.016187,25
    .zV]>g`9`U*#,0.047185,50.952499,0.003451,0

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    Just for clarity, I'm just trying to get the function to not return SUN_DATA_ERR on this file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21