-
reading files
Alright, i've got a problem! my program reads a text file that has x and y co-ordinates for a number of points on a graph, i can read the file ok, but need to find the max and min values for x and y thats where it goes wrong!
this is what i'e got:
Code:
void getPoints(void)
{
char buf[100];
int length =0;
int a;
int b;
int v=0;
int i =0;
FILE *stream;
if ((stream = fopen("Points.txt","r")) == NULL)
{
fprintf(stderr,"Error opening file.");
exit(1);
}
while(!feof(stream))
{
fgets(buf,30,stream);
if (sscanf(buf,"%d %d", &a,&b))
{
pointX[i] = a;
pointY[i] = b;
i++;
}
}
Max_X = pointX[0];
Min_X = pointX[0];
Min_Y = pointY[0];
Max_Y = pointY[0];
for(v=2;v=35266;v++)
{
if(Max_X < pointX[v] ) Max_X = pointX[v];
if(Min_X > pointX[v] ) Min_X = pointX[v];
if(Max_Y < pointY[v] ) Max_Y = pointY[v];
if(Min_Y > pointY[v] ) Min_Y = pointY[v];
}
}
i cann't get it to scan through all the coordinates correctly. any help appreciated!
-
It seems ok. How do you declare pointX and pointY? Perhaps your indexes are wrong (an array with n elements range from 0 to n-1)?
-
declared like this:
Code:
int pointX[36000];
int pointY[36000];
int Max_X,Min_X,Max_Y,Min_Y;
when i compile the max values have some random number in them and te min values are always 0,
-
As always, the error is right in front of the nose ;).
Code:
for(v = 2; v = 35266; v++)
Should be
Code:
for(v = 2; v < 35266; v++)
(Why start at 2? That's the third element)
If that wasn't the problem, perhaps you're compiling in a 16-bit compiler? Integers then range from ~ -32000 to +32000, making it impossible to address 35266.