简体   繁体   English

将文件读取到C中的2D数组

[英]Read a file to a 2D array in C

I'm learning C and I decided to make a text game as my learning project . 我正在学习C语言,我决定做一个文字游戏作为我的学习项目 So I'm trying this primitive "parser" that reads a text file into a 2D array, but there's a problem: this map doesn't use 1 character-wide cells, it uses 2 character-wide cells. 因此,我正在尝试使用将文本文件读入2D数组的原始“解析器”,但是存在一个问题:此地图不使用1个字符宽的单元格,而是使用2个字符宽的单元格。 For instance, the player is represented with a ** , a door is represented like ## and so on. 例如,玩家用**表示,门用##表示,依此类推。

That means, I need to read 2 characters of the text file and then assign it to the respective cell in the map. 这意味着,我需要读取文本文件的2个字符,然后将其分配给地图中的相应单元格。

Well, I made a test file that contains 好吧,我做了一个测试文件,其中包含

aabbccddee
ffgghhiijj
kkllmmnnoo
ppqqrrsstt
uuvvwwxxyy

And I tried to read it with 我试图用

#include <stdio.h>

#define ROWS 5 
#define COLS 5

int main() {
    FILE *mapfile = fopen("durr", "r");
    char charbuffer[3], row, col, *map[ROWS][COLS];

    /* Initializing array */
    for (row = 0; row < ROWS; row++) {
        for (col = 0; col < COLS; col++) {
            map[row][col] = "  ";
        }
    }

    /* Reading file into array */
    for (row = 0; row < ROWS; row++) {
        for (col = 0; col < COLS; col++) {
            map[row][col] = fgets(charbuffer, 3, mapfile);
        }
    }

    /* Printing array */
    for (row = 0; row < ROWS; row++) {
        for (col = 0; col < COLS; col++) {
            printf("%s", map[row][col]);
        }
        printf("\n");
    }

    fclose(mapfile);    
    return 0;
}

But when I execute it, I get this 但是当我执行它时,我得到了

uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu

I think it has something to do with the fact that fgets return a pointer, but I'm not sure. 我认为这与fgets返回一个指针有关,但是我不确定。 I tried doing it with fgetc but it looks messy and I dropped it after reading about gets. 我尝试用fgetc来做,但是看起来很乱,在阅读了关于get的内容后就将其删除了。

You should have strdup'ed the contents read from the file. 您应该已经整理了从文件中读取的内容。 Replace the file reading block with this: 以此替换文件读取块:

/* Reading file into array */
for (row = 0; row < ROWS; row++) {
    for (col = 0; col < COLS; col++) {
        if (fgets(charbuffer, 3, mapfile))
            map[row][col] = strdup(charbuffer);
    }
}

and don't forget to put this at the beginning of your code too: 并且不要忘了也将其放在代码的开头:

#include <string.h>

The main problem is that 主要的问题是

char *map[ROWS][COLS];

only allocates space for ROWS x COLS char pointers , it does not allocate space for the strings themselves. 仅为ROWS x COLS char 指针分配空间,不为字符串本身分配空间。 So you're continually overwriting charbuffer as you've done it with every iteration, which explains why you end up with the same characters repeated over and over when you read out. 因此,每次迭代完成后,您都将不断覆盖charbuffer,这解释了为什么在读取时最终会重复出现相同的字符。 You need to dynamically allocate the memory you need, along the lines of 您需要按照以下方式动态分配所需的内存:

for (row = 0; row < ROWS; row++) {
    for (col = 0; col < COLS; col++) {
        charbuffer = (char*)malloc(sizeof(char) * 3);  //****
        map[row][col] = fgets(charbuffer, 3, mapfile);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM