簡體   English   中英

如何在C中連續寫入和讀取相同文件

[英]How do I continuously write to and read from the same file in 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 這很有風險,因為您很有可能在基本信息之上進行重寫。 但是按照這個答案 ,你應該會好的。

如果您對fseek的工作方式感到好奇,也請查看信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM