Displaying data from an array of files.

/*---------------------------------------------------------------*/
/* When I compile this program, I get the correct output for the first data file. But on the second output, the get_range value is incorrect, but the real-filesize value is still correct. Any help? */


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "lab6.h"

const char *FILENAME[] = 	/* array of the data file names */
    {"lab6a.dat", "lab6b.dat", NULL };

/*---------------------------------------------------------------*/
int main(void)
{
    int file_count = 0;         /* keeps track of which file we are on */
    int check_overflow = 0;     /* counter to prevent array overflow   */
    int real_filesize = 0;      /* actual count of the values read     */
    int number_list[MAX_SIZE];  /* the array                           */
    int range_count;         /* count of numbers within the range   */



    /* loop through each file, reading it, and getting stats on it */

    for (file_count=0; FILENAME[file_count] != NULL; file_count++)
    {
       real_filesize = get_data( FILENAME[file_count], number_list );

       get_range_count(number_list, real_filesize, &range_count);

       print_all(FILENAME[file_count], real_filesize, &range_count);

    }
    return EXIT_SUCCESS;
}
/*-----------------------------------------------------------------*/
/* This function will open the input file, read the data into      */
/* array k, and return real_filesize.                              */

int get_data(const char *filename,  /* input, current file name */
	     int number_list[])     /* output, the filled array */
{
    FILE * input_file;
    int real_filesize=0;

    input_file = fopen(filename, "r");
    if (input_file == NULL)
    {
	printf("Error on file-open of file %s", filename);
	exit(EXIT_FAILURE);
    }

    while ((fscanf(input_file, "%i", &number_list[real_filesize])) == 1)

	{
	   real_filesize += 1;
	}

    return real_filesize;
}

void get_range_count(int number_list[], int real_filesize, int *range_count)
{
    *range_count = 0;
    int i;
    for (i = 0; i < real_filesize; i+= 1)
    {
        if (number_list[i] >= 90 && number_list[i] <= 99)
        {
            *range_count += 1;
        }
    }
    return 0;
}

/*-----------------------------------------------------------------*/
/* This function will print all but your name (which is above)     */

void print_all(	const char *filename,  /* input, the current filename    */
		int real_filesize,     /* input, actual size of the data */
		int *range_count)   /* input, # of values in range    */
{
    int d;

    printf("\n\n.  Lab 6. \n\n");
    printf("\nFile %s:  ", filename);
    printf("\nThere are %i values in the range of 90 through 99\n",*range_count);
    printf("out of a total of %i values.", real_filesize);

    printf("\n\n");

    return;
}
/*-----------------------------------------------------------------*/
Last edited on
First of all, you need to put fclose() somewhere in int get_data()
Also you're doing return 0; in void get_range_count() even though its a void function.

However, those are not the errors that would have caused range_count to be incorrect so if there is an issue it must be something else. To me your code looks correct so far so perhaps you should post an example.
So it actually works, just a grammatical error on my part. Thank you for the response though!
Topic archived. No new replies allowed.