home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
CPROG
/
CNEWS018.ZIP
/
BEGINNER.ZIP
/
GET_DATA.C
< prev
next >
Wrap
Text File
|
1989-11-14
|
1KB
|
37 lines
/* 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 <stdio.h>
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 */
}