IHYPRESS.NET > C PROGRAMMING
HOME | ASP | C | HTML/CSS | JAVASCRIPT | MATLAB | PERL | PHP | PYTHON | RUBY
C > 07. Arrays I (1D) [PROGRAM #06]
CODERESULT
<
/* Problem: This program partially fills an array from a file 
until the end of file (EOF). 
We get the actual number of data read  */

#include <stdio.h>
#define SIZE 100 /* size of the array */
 
int
array_from_file (double a[], int size)
{
        int i;
        FILE* in;
 
        in = fopen ("data_array.dat", "r");
        
        i=0; /* the first cell */
 
        /* filling the array cell by cell */
        /* until it is full or until the EOF */
        while (i < SIZE && fscanf (in, "%lf", &a[i]) != EOF)
        {
               i=i+1;
        }

        fclose (in);

        /* the actual number of values in the array */
        return (i);       
}
 
 
int
main (void)
{
        double array[SIZE];
        int actual_size, i;
 
        actual_size = array_from_file (array, SIZE);
 
        for (i=0; i < actual_size; ++i)
               printf ("%3.1lf ", array[i]);
 
        printf ("\nThe array contains %d values ", actual_size);
        printf ("for a capacity of %d cells.\n", SIZE);
 
        return (0);
}

Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
43.3 56.1 23.4 87.5 123.2 13.4 77.1
The array contains 7 values for a capacity of 100 cells.
>
COPYRIGHT © 2019 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.
SHARE