Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello All,
I am doing an game of life program in C lang. I'm stuck on how to read in values from a given file into a 2d array. I'm also having trouble on displaying the array in an interal of 5 until I reach a certain generation. I know I have to use a for loop. Please help soon :-)

void print_el(char *s, char *fmt);
char *ptr = NULL;
char *farray[500][50];int main(int argc, char *argv[]) {
FILE *fpt;
int i = 0,j = 0;/* start init */
ptr = malloc(256);
fpt = fopen(argv[1],"r");
/* end init */if (fpt && ptr) {
while ( (fgets(ptr,256,fpt)) != NULL) {
i = j > 49 ? i + 1 : i;
j = j > 49 ? 0 : j + 1;
printf("%d %d: ", i, j);
farray[i][j] = ptr;
print_el(farray[i][j],"Array element: %s\n");
free(farray[i][j]);
ptr = malloc(256);
}
fclose(fpt);
return 0;
} else {
perror("Init");
return 1;
}
return 1;
}void print_el(char *s, char *fmt) {
printf(fmt, s);
}How you want to split up your strings,
to arrange for key::value pairs between % 5 countsallocate memory, clean up,etc.. all depends on you. This is a quick and very dirty read and storage and print and free
example.

![]() |
VB String Manipulation
|
c++ constant declaration
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |