I'm pretty new to programming with basic knowledge in C & C++. I've been trying to run a code on my .dat file that I am using for image analysis.
Basically this .dat file has x & y positions of clusters along with their radius. With this code I am trying to divide my image (which is 200mm x 300mm in dimension, after scaling the pixels) into grids of 5mm x 5mm and find the number of points having radius=1 and the number of points having radius=100.
Here's my code:
#include<stdio.h>
int main()
{
int i=0,g,h,ng[350][250],ns[350][250],ntot[350][250];
float a[6001],b[6001],c[6001];
FILE *fres1, *fres2;
for(g=5;g<=350;g=g+5)
{
for(h=5;h<=250;h=h+5)
{
ng[g][h]=0;
ns[g][h]=0;
ntot[g][h]=0;
}
}
fres1 = fopen("trial_final_1.dat","r+");
fres2 = fopen("result_boxes.dat","w");
if(fres1 == NULL)
{
printf("Error!! Cannot open file \n" );
return 1;
}
else
{
printf("File opened successfully, kindly check your folder for result_boxes file :) \n");
fprintf(fres2,"x(mm) y(mm) no. of glass beads no. of stain-less steel balls total no. of balls \n");
while(!feof(fres1))
{
fscanf(fres1,"%f %f %f",&a[i],&b[i],&c[i]);
g = ((a[i]/5) + 1)*5;
h = ((b[i]/5) + 1)*5;
if(c[i] == 100)
{
ng[g][h] = ng[g][h]+1;
}
else
{
ns[g][h] = ns[g][h] + 1;
}
ntot[g][h] = ntot[g][h] + 1;
++i;
}
g = 5;
h = 5;
for(g=5;g<=350;g=g+5)
{
for(h=5;h<=250; h=h+5)
{
fprintf(fres2,"%d %d %d %d %d\n",g,h,ng[g][h],ns[g][h],ntot[g][h]);
}
}
}
fclose(fres1);
fclose(fres2);
return 0;
}
My input .dat file (i.e. 'trial_final_1.dat') contains data as:
150.505951 0.797619 100.000000
172.327438 5.976130 100.000000
137.538651 11.089217 100.000000
151.304276 10.139803 100.000000
137.008926 13.175000 100.000000
120.400734 13.943015 1.000000
136.759262 14.199074 100.000000
On running my code, initially I get the output "File opened successfully, kindly check your folder for result_boxes file :)", but after this I get a message saying that file.exe has stopped working. Please help me troubleshoot this problem. I've also tried using while(fscanf(fres1,"%f %f %f",&a[i],&b[i],&c[i])!=EOF )
instead of while(!feof(fres1))
, but the output in both the cases remains the same.