Reputation: 23
I've been set on a task to read the list of numbers from an external file and display statistics on it. My initial plan was to count how many times a specific value has appeared in the code but I encountered two problems: my output of the external file was different to the values on the file and I'm not able to count the individual numbers in this code using the for loop down below. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *fpointer;
fpointer = fopen("randice.txt","r");
char filename[600];
int v, c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0;
while (!feof(fpointer))
{
fgets(filename,600,fpointer);
puts(filename);
}
for(v=0;v<600;v++)
{
if (filename[v] == 1)
c1++;
if (filename[v] == 2)
c2++;
if (filename[v] == 3)
c3++;
if (filename[v] == 4)
c4++;
if (filename[v] == 5)
c5++;
if (filename[v] == 6)
c6++;
}
fclose(fpointer);
return 0;
}
Upvotes: 2
Views: 109
Reputation: 9432
Word frequency statistics in C:
A more advanced example is in https://rosettacode.org/wiki/Word_frequency#C
Upvotes: 1