2

I am trying to store the coordinates (x, y) in a text file, where x denotes the row number and y the column. I will then use this information to change the element of the array to 1, and the others will remain at 0. I want to do this multiple times to the same .txt file. I'm trying to simulate the famous Game of Life. The trouble is that is only successfully reads from the file once so the array cannot update, and so the array just prints off the same stuff as before.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 30
#define COLS 20
#define ITERATIONS 6

int fillBoard(int b[ROWS][COLS]);
int newRound(FILE *i, int b[ROWS][COLS]);
int cellAliveOrDead(FILE *p, int b[ROWS][COLS]);
int cellBirth(FILE *p, int b[ROWS][COLS]);


int main(void) {

    int board[ROWS][COLS];
    char fileName[] = "#life 1.06";
    int i, j;
    int x, y;
    int round = 0;
    FILE *fp, *ifp;
    char fileFormat[11];
    int p, o;

    fp = fopen("life.txt", "r");
    ifp = fopen("life2.txt", "r");

    if (fp == NULL) {
        printf("Error\n");
        exit(1);
    }

    if (ifp == NULL) {
        exit(1);
    }

    fillBoard(board);
    fgets(fileFormat, 11, fp); // read input from first line
    if (strcmp(fileName, fileFormat) == 0) {
        while (fscanf(fp, "%d %d", &x, &y) == 2) {
            board[x][y] = 1;
            printf("x:%d y:%d\n", x, y);
        }
    } else {
        printf("Wrong file");
    }

    // print game with the starter cells
    for (i = 0; i < ROWS; i++) {
        printf("\n");
        for (j = 0; j < COLS; j++) {
            printf("%d", board[i][j]);
        }
    }

    newRound(ifp, board);

    fclose(fp);
    fclose(ifp);
    return(0);
}

int newRound(FILE *ij, int b[ROWS][COLS]) {

    int round = 0;
    int x, y;
    int i, j;

    while (round < ITERATIONS) {
        printf("\n");
        cellAliveOrDead(ij, b);
        cellBirth(ij, b);
        fillBoard(b);
        while (fscanf(ij, "%d %d", &x, &y) == 2) {
            b[x][y] = 1;
            printf("x:%d y:%d\n", x, y);
        }
        round++;
        // print game round 2
        for (i = 0; i < ROWS; i++) {
            printf("\n");
            for (j = 0; j < COLS; j++) {
                printf("%d", b[i][j]);
            }
        }
    }
}

int fillBoard(int b[ROWS][COLS]) {

    int i, j;
    for(i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            b[i][j] = 0;
        }
    }
}

int cellAliveOrDead(FILE *p, int b[ROWS][COLS]) {

    int count;
    int i, j;

    for(i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            count = 0;
            if(b[i][j]  == 1) {
            if(b[i-1][j] == 1) count++;
            if(b[i+1][j] == 1) count++;
            if(b[i][j-1] == 1) count++;
            if(b[i][j+1] == 1) count++;
            if(b[i-1][j-1] == 1) count++;
            if(b[i-1][j+1] == 1) count++;
            if(b[i+1][j-1] == 1) count++;
            if(b[i+1][j+1] == 1) count++;
            if(count == 2 || count == 3) {
                //b[i][j] = 1;
                fprintf(p, "%d %d\n", i, j);    
            }
            }
        }
    }
}

int cellBirth(FILE *p, int b[ROWS][COLS]) {

    int count;
    int i, j;

    for(i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            count = 0;
                if (b[i][j] == 0) {
                    if(b[i-1][j] == 1) count++;
                    if(b[i+1][j] == 1) count++;
                    if(b[i][j-1] == 1) count++;
                    if(b[i][j+1] == 1) count++;
                    if(b[i-1][j-1] == 1) count++;
                    if(b[i-1][j+1] == 1) count++;
                    if(b[i+1][j-1] == 1) count++;
                    if(b[i+1][j+1] == 1) count++;
                    if (count == 3) {
                        //b[i][j] = 1;
                        fprintf(p, "%d %d\n", i, j);
                    }
                }
        }
    }
}
traderjosh
  • 321
  • 5
  • 16
  • 3
    Read the manual page for `fopen`. There are more options than just `"r"`. You'll also want to look up `fseek`. Use with caution. If you read some data, then you want to write new data over it, you have to be mindful of how many bytes you're writing versus what you think you're updating. You can easily overwrite something else. – lurker Oct 23 '15 at 19:31
  • 1
    Possible duplicate of [File read write to same file?](http://stackoverflow.com/questions/5787867/file-read-write-to-same-file) – user2736738 Oct 23 '15 at 19:33
  • I've tried "r+", "w" and "w+". I've also tried to use fseek, but that didn't work either. I'll try fseek again though, as I think that is my best option. Thanks for the heads up about fseek. – traderjosh Oct 23 '15 at 19:37

1 Answers1

0

The simplest answer would be to use 2 files.

Write into a FILE* A. Then close it and read it as FILE* B, and write into a FILE* A. Repeat.

The other method would be to use fseek. This is quite risky as the chances of you rewriting on top of essential information is quite possible. But follow this answer, and you should be fine.

Also see this if you are curious about how fseek works.

Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27