Array : Filling Partially

/* 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>
 
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 < 100 && 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[100];
        int actual_size, i;
 
        actual_size = array_from_file (array, 100);
 
        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 100 cells.\n");
 
        return (0);
}

২টি মন্তব্য:

  1. /*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)
    int cot (double a[], int size)
    {
    int j=0;
    FILE *fl;
    fl=fopen ("st.txt","r");
    while (j<100)
    {
    if(!feof(fl))
    {
    fscanf (fl,"%lf",&a[j]);
    }
    else
    {
    break;
    }
    j+=1;
    }
    fclose (fl);
    return (j);
    }
    int main (void)
    {
    double ar[100];
    int acsize, j;
    acsize=cot(ar,100);
    for (j=0;j< acsize;j++)
    printf ("%4.1lf ", ar[j]);
    printf ("\nThe array contains %d values and", acsize);
    printf ("capacity of 100 cells.\n");
    getch();
    }

    উত্তরমুছুন
  2. #include
    int main()
    {
    int a[15],i;
    FILE *i;
    i=fopen("Arif.txt","r");
    for(i=0;i<15;i++)
    fscanf(i,"%d",&a[i]);
    printf("The numbers are:");
    for(i=0;i<15;i++)
    printf ("%5d",a[i]);
    fclose(i);
    }

    উত্তরমুছুন