NO

Author Topic: need help reading and printing data file  (Read 6349 times)

doja

  • Guest
need help reading and printing data file
« on: May 23, 2010, 09:22:37 PM »
this program is suposed to read in data then write it to a data file (name, weight, hieght,ect....). and then give the user the option to print all of the info from the data file to the screen. problem is it only prints one one of the data entries. my instructor wants me to use a (-99) somehow in the data file to make it stop reading at the end of the file and then print the results to the screen




#include <stdio.h>
#include <math.h>
#define SENTINEL -99
int main(int argc, char *argv[])
{
      int number,ok;  //used for switch & while statement to run program
           float wt_lb;  //weight in pounds
      float ht_in;  //height in inches
              float square;
             float bmi;  //body mass index
      float ws;  //walking stride
      float mw;  //minutes walked
      float mph=0;  //walking speed
      float cb;  //calories burned
      float dw;  //distance walked in miles
      char fname [10];  //first name
      char lname [15];  //last name
           char ss[10] = "-99";
   ok=strcmp (fname,ss);
FILE   *outfile, *infile;

   //<HEADER>
      printf("\n\n         PEDOMETER CALCULATION\n\n");
      printf("\n***********************************************************************\n\n");
      printf(" This program will allow you to enter information about yourself,\n\n\t");
      printf(" save it, and then be able to retreive it.\n\n\t");
      printf("'You must first enter your information before you retreive it'\n\n");
      printf("************************************************************************\n\n");
      printf(" \n Please select from the following 3 options:\n\n\n");

   //<SWITCH & LOOP STATEMENT TO RUN PROGRAM>
      while (number != SENTINEL)
{
      printf("\t* Enter '1' to input your data, '2' to read your data\n\n\t\tor '-99' to end program: ");
      scanf("%d",&number);
      printf("\n\n");
      if (number ==-99)
      printf("\t\t\t< You have ended this program >\n\n\n\t\t\t   ");
      else if (number != -99)
      switch (number)
   {   
      case 1:
         while (number != SENTINEL)
            {
            printf("name enter '1' to continue or '-99' to exit\n");
            scanf("%d", &number);
            if (number != -99)
                                 {       
                            outfile = fopen ("test_data.gdb", "a");
   //<NAME INPUT>   
      printf(" Please enter your first name: ");
      scanf("%s",fname);
      printf("\n");
      printf(" Please enter your last name: ");
      scanf("%s",lname);
      printf("\n");


   //<WEIGHT AND HEIGHT INPUT >
             printf (" Please enter your weight in pounds: ", wt_lb);
           scanf (" %f",&wt_lb);
      printf("\n");
      printf (" Please enter your hieght in inches: ", ht_in );
      scanf (" %f", &ht_in);
      printf("\n");


   //<WALKING INPUT>
      printf(" Please enter your walking stride in inches: ");
      scanf("%f", &ws);
      printf("\n");
      printf(" Please enter the time in minutes you have walked: ");
      scanf("%f", &mw);
      printf("\n");
      
      printf("\n\n\n");
      printf("*****************************************************************************\n\n\t");
fprintf(outfile, "first name-%10s\n last name-%15s\n weight %3.0f\n height-%2.0f\n stride- %3.0f\n minutes walked-%3.0f\n", fname, lname, wt_lb, ht_in, ws, mw);
fclose (outfile);   // close the data file
               }
                  else if(number==-99)
                  {
printf("\t* Enter '1' to input your data, '2' to read your data\n\n\t\tor '-99' to end program: ");
      scanf("%d",&number);
      printf("\n\n");
      if (number ==-99)
      printf("\t\t\t< You have ended this program >\n\n\n\t\t\t   ");
      else if (number ==1);
      //switch (number)
                     }
}
     break;
            case 2:
               

infile = fopen ("test_data.gdb", "r");      // open the file to read the data
      fscanf(infile,"%10s %15s %3.0f 2.0f %2.0f %3.0f\n", fname, lname, wt_lb, ht_in, ws, mw);    // read the data
      
   //<BMI FORMULA>
           square= ht_in * ht_in;
      bmi= 703*wt_lb/square;

       if (bmi < 18.5)
      {
      printf ("* You are underweight, ");
      }
      else if (bmi<24.9)
      {
       printf ("* Your weight is normal, ");
      }

      else if (bmi<29.9)
      {
      printf ( "* You are overweight, ");
      }

      else if (bmi >30.0)
            {
                  printf ( "%10s are obese, ", fname);
       }


   //<CALORIES FORMULA>
      if (mph<3)
      {
      cb=mw*((10.5*wt_lb/2.2)/200);
      printf("you have burned %7.2f calories,\n\n",cb);
      }
      else if (mph <4.6)
      {
      cb=mw*((21*wt_lb/2.2)/200);
      printf("you have burned %7.2f calories,\n\n",cb);
      }
      else if (mph > 4.5)
      {
      cb=mw*((24.5*wt_lb/2.2)/200);
      printf("you have burned %7.2f calories,\n\n",cb);
      }

   //<DISTANCE IN MILES FORMULA>
      dw=(mw/60)*mph;
      printf("\tand you have walked %7.2f miles *\n\n\n",dw);
      printf("******************************************************************************\n\n\t");

      fclose (infile);   // close the data file

   

}
   
   }
      
    return 0;
}





kingboyD

  • Guest
Re: need help reading and printing data file
« Reply #1 on: December 24, 2010, 06:05:33 PM »
// Try something like this:


   FILE * stream, *data;
   int i, x = 0 ;
   char temp[255] ;
   char * c ;


   if ((data = fopen( datafile, "wt")) == NULL)
    { // Error
      return(errorno);
   }


   // read stream and parse
   while ( ( fgets( temp, 384, stream ) ) != NULL )
    {
     ++x;
      c = temp ;   // check first char
      if (( *c=='%' )||(*c==';')||(*c=='#'))   // it's a comment?
      continue ;
     i = strlen( temp );   // how long is line?
     if ( i < 3 )         // too short
      continue ;
     c = &temp[--i];      // point at last char
     if ( *c == '\n' )      // if it's a newline,
     *c = '\0';         // strip it off

[...]


The content of the line is in c