I have a basic C program that I have to do for a Personal Software Processes assignment. I'm fairly new to C so I honestly can't see where I've gone wrong. Please read through and let me know what I'm doing that's causing it to crash?
I've got to read in a file, and store the values as an array. It comes up with no compiling errors, just a segmentation fault.
#include <stdio.h>
#define array_limit 100
int main (void)
{
FILE *ifp;
char *mode = "r";
ifp = fopen("samplepopulation.txt", mode);
if (ifp==NULL)
{
printf("cannot read file \n");
}
else
{
int i;
float sample;
float values[array_limit];
i = 0;
do
{
fscanf(ifp, "%f", &sample);
if (!feof(ifp))
{
values[i] = sample;
printf("%f \n", values[i]);
i++;
if (i>array_limit)
{
printf("File larger than allowed/n");
break;
}
}
else
{
printf("read complete");
}
} while (ifp!= EOF);
}
fclose(ifp);
return 0;
}