www.pudn.com > λ²Ù×÷.zip > GET_DATA.C


/* GET_DATA.C */ 
/* Read in 4 numbers from a file */ 
/* Data passed to this function: none */ 
/* Data returned from this function: The sum of 4 real numbers */ 
   
#include   
   
float get_data(int *samp_size) 
{ 
   FILE *in_file;      			/* declare an area for a file */ 
   float sum_x = 0.0;			/* THIS WAS IN THE ABOVE CODE */ 
   float in_val;     	 		/* declare an intermediate variable to */ 
               		         /* be read in */ 
   int i;              			/* a counter variable */ 
   
   if ((in_file = fopen ("mydata.dat", "r+t")) == NULL) 
   { 
      printf ("Oops, I don\'t see the data file\n"); 
      exit(1); 
   } 
   
   do {                       /* set up a loop to read in the file */ 
   
      fscanf (in_file, "%f", &in_val);   /* read the file */ 
      sum_x += in_val;    		/* add the value to the old sum */ 
      (*samp_size)++;			/* increment the counter */ 
	   }  while (in_val >= 0); 
   
   (*samp_size)--;            /* we have to decrement this because */ 
   sum_x -= in_val;        	/* tried to read in the stop number */ 
                              /* and add in the stop number to the sum */ 
   
   fclose (in_file);        	/* close the file */ 
   return (sum_x);          	/* return the sum to the main */ 
                              /* program */ 
}