-3

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.

Wilbur Vandrsmith
  • 5,040
  • 31
  • 32
  • Run it in the debugger so you can see where it's crashing. This means you probably used some memory that's not yours. Like your file was bigger than the 6001 elements you allocated for a/b/c or something like that. – xaxxon Jun 11 '13 at 05:20
  • 2
    Also, with meaningless variable names like that, no one is going to want to read your code. Lose some vertical whitespace, trim the parts that aren't required to reproduce the error, give meaningful variable names and update your question. – xaxxon Jun 11 '13 at 05:21
  • hey, i'll keep ur suggestion in mind while asking a question next time.....thnx for ur time:) – user2470358 Jun 11 '13 at 15:52

1 Answers1

1

You are not doing any bounds-checking on your arrays. A quick skim of your code shows that you are overrunning the bounds of ng, ns and ntot with this loop:

for(g=5;g<=350;g=g+5)
{ 
    for(h=5;h<=250; h=h+5)
    {
        ...
    }
}

The problem above is that you are using <= instead of <, or that your array dimensions are one element too small.

You also don't check if fres2 is a valid file handle before writing to it, nor do you check that your calls to fscanf succeeded. For example:

fscanf(fres1,"%f %f %f",&a[i],&b[i],&c[i]);           
g = ((a[i]/5) + 1)*5;
h = ((b[i]/5) + 1)*5;

If the above call fails, you may be using uninitialised values for a[i] or b[i], which you then go on to use as array indices without any bounds-checking. It's a recipe for disaster, and it's probably always going to happen because your loop checks for EOF before you attempt to read.

At the very least you should do this:

if( 3 != fscanf(fres1,"%f %f %f",&a[i],&b[i],&c[i]) ) break;

And one more thing... What do you think happens when i reaches a value of 6001?

paddy
  • 60,864
  • 6
  • 61
  • 103
  • Thank you so much for such a quick reply.....the array dimension was falling short.....the program is running fine now....thnx for ur time:) – user2470358 Jun 11 '13 at 15:50