c - 我如何连续写入和读取 C 中的同一个文件

标签 c

我试图将坐标 (x, y) 存储在文本文件中,其中 x 表示行号,y 表示列。然后我将使用此信息将数组的元素更改为 1,而其他元素将保持为 0。我想对同一个 .txt 文件多次执行此操作。我正在尝试模拟著名的生命游戏。问题是它只从文件中成功读取一次,所以数组无法更新,所以数组只是打印出与以前相同的内容。

这是我的代码:

#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);
                    }
                }
        }
    }
}

最佳答案

最简单的答案是使用 2 个文件

写入FILE* A。然后关闭它并读取它作为FILE* B,写入一个FILE* A。重复。

另一种方法是使用fseek。这是非常冒险的,因为您很有可能在基本信息之上重写。但是按照这个answer ,你应该没问题。

另见 this如果您对 fseek 的工作原理感到好奇。

关于c - 我如何连续写入和读取 C 中的同一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33310069/

相关文章:

c - 对给定值进行排序的最佳算法

c - request_irq中的dev_id参数是什么?

java - 在 JNI 中创建整数对象

c - 下面文法的左推导和右推导

c - 在 C 中声明局部函数的惯用方法

编译一个简单的设备驱动程序代码

c - 如何fwrite到指针的指针?

c++ - 从 void* 到 unsigned char* 的指针行为的奇怪案例

c - -Wpedantic 类型参数在转换后增加的错误类型

c - 改进我的链接列表程序