So currently my program uses a hard-coded array like this:
char *array[] = {"array","ofran","domle","tters", "squar"}
Basically n strings of n length "an n*n grid. I then treat the values like a 2D array. So I will access array[y][x] and do comparison operations and math using the corresponding ASCII.
I wanted to allow text files of various sizes (n*n) (up to 32) be implemented in my program instead of hard coding it. But am having issues with using fgets.
My current function for getting and storing the file information looks like this:
char *array[32];
char buffer[32];
FILE *fp = fopen("textfile.txt","r");
int n = 0;
while(fgets(buffer, 32, fp)){
array[i] = buffer;
n++;
}
fclose(fp);
but all values of "array" are the same (they are the last string). So with the example values above. If I printed array[0] to array [4] I get
values from my code
squar
squar
squar
squar
squar
expected values:
array
ofran
domle
tters
squar
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…