将文件读取到C中的二维数组 [英] Read a file to a 2D array in C

查看:406
本文介绍了将文件读取到C中的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C语言,并且决定把文本游戏作为我的学习项目。所以我正在尝试这个原始的解析器,它将文本文件读取到二维数组中,但有一个问题:该映射不使用1个字符的单元格,而是使用2个字符的单元格。例如,玩家用 ** 表示,门表示为 ## 等等。 / b>

这意味着,我需要读取文本文件的2个字符,然后将其分配给映射中的相应单元。


$ b $我做了一个测试文件,其中包含

  aabbccddee 
ffgghhiijj
kkllmmnnoo
ppqqrrsstt
uuvvwwxxyy

我试着用

  #include< stdio.h> 

#define ROWS 5
#define COLS 5

int main(){
FILE * mapfile = fopen(durr,r );
char charbuffer [3],row,col,* map [ROWS] [COLS]; (行= 0;行 for(col = 0; col< COLS; col ++){}的初始化数组* /
{


map [row] [col] =; (行= 0;行 for(

/ *将文件读入数组* /
col = 0; col< COLS; col ++){
map [row] [col] = fgets(charbuffer,3,mapfile); (行= 0;行 for(col = $;



/ *打印数组* /
0; col< COLS; col ++){
printf(%s,map [row] [col]);
}
printf(\\\
);
}

fclose(mapfile);
返回0;
}

但是当我执行它时,我得到这个

  uuuuuuuuuu 
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu

我认为这与fgets返回一个指针有关,但我不确定。我试着用fgetc来做,但是看起来很乱,我在阅读了get之后就放弃了它。

从文件中读取的内容。用这个替换文件读取块:

  / *将文件读入数组* / 
for(row = 0; (col = 0; col if(fgets(charbuffer,3,mapfile))
map [row] row(row; row ++){
col] = strdup(charbuffer);


code
$ b

不要忘记把它放在开头

  #include< string.h> 


I'm learning C and I decided to make a text game as my learning project. 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. 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.

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. I tried doing it with fgetc but it looks messy and I dropped it after reading about gets.

解决方案

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>

这篇关于将文件读取到C中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆