arrays, file access question..a little bit of everything
Well i did make a quick search throughout the forum for an answer ..iam sure there is one somewhere..maybe iam not so good at searching...anyway here is my question...I think iam fairly good at c++ but lately i've decide to learn c as well...why not? so i've decided to port one of my programms..but iam stuck (it is obvious i suppose) ok..here is the code.
Code:
#include <stdio.h>
int linesoffile(FILE *stream)
{
printf("file read\n");
char x[12],y[12],z[12];
int i=0;
char *forpop = "%s %s %s\n";
while (fscanf (stream, forpop, x, y, z) == 3)
{
printf("x:%s, y:%s, z:%s\n", x, y, z);
i++;
}
printf("the number of coordinates in the file are: %d", i);
return i;
}
int main (int argc, const char * argv[])
{
FILE *stream = fopen("stone2000.xyz", "r");
if(!stream)
{
printf("file open failed");
}
int i = linesoffile(stream);
printf("\n%d ", i);
float x[i], y[i], z[i], b;
int g;
char * del = "%f %f %f\n";
for ( g = 0; i > g; g++);
{
b = fscanf (stream, del, x[g], y[g], z[g]);
g++;
}
//testing the arrays
printf("%f %f %f %d", x[0], y[45], z[78], g);
fclose(stream);
return 0;
}
ok this piece of code displays the file correctly but fills the arrays with zeros...so i've thought that i need to rewind the stream (or something) but it still doesn't work. if i access the linesoffile function the first time i get the correct result but not the second etc...i reckon it is the same problem..
any ideas?
ps. i don't think that it matters but iam using a mac