简体   繁体   English

如何从文件加载二维字符数组并使用 ncurses.h 在 C 中使用 mvprintw() 打印它?

[英]How to load 2D char array from file and print it with mvprintw() in C using ncurses.h?

I'm coding a small game and i decided to load a map from the file but I'm facing a problem.The file looks like this:我正在编写一个小游戏,我决定从文件中加载 map 但我遇到了问题。文件如下所示:

在此处输入图像描述

and i need to print it like it is in the file expect of dots.我需要像在点的文件中一样打印它。 They has to be spaces.它们必须是空格。 my code for print looks like this:我的打印代码如下所示:

void printmap(){
    clear();
    refresh();
    FILE *mapfile;
    int width=30;
    int height=20;
    char map[20][30];
    mapfile = fopen(mapname, "r+");
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                mvprintw(0,0,"             ");
                mvprintw(0,0,"%d %d",row,col);
                refresh();
                map[row][col]=fgetc(mapfile);
            }
        }
    fclose(mapfile);
    
    offsetW=(xMax-width)/2;
    offsetY=((yMax-height)/2)-3;
    printOffsetW = offsetW+23;
    printOffsetY = offsetY+17;
        
        for(int i=0;i<20;i++){
        offsetW=(xMax-width)/2;
        
        for(int y=0;y<width;y++){
            if(map[i][y]=='#'){
                attron(COLOR_PAIR(1));
                mvprintw(i,y,"#");
                attroff(COLOR_PAIR(1));
            }
            else if(map[i][y]=='*'){
                attron(COLOR_PAIR(2));
                mvprintw(i,y,"*");
                attroff(COLOR_PAIR(2));
            }
            else if(map[i][y]==' '||map[i][y]=='.'){
                mvprintw(i,y," ");
            }
            
            offsetW++;
        }
        offsetY++;
    }
   
    mvprintw(printOffsetY,printOffsetW,"@");
    refresh();
}

Offsets are there just for centering the map (in the future), so you can ignore them.偏移量仅用于将 map(将来)居中,因此您可以忽略它们。

My actual print looks like this:我的实际打印如下所示:

在此处输入图像描述

And I can't really tell, where the problem is.我真的不能说,问题出在哪里。

I would appreciate any help.我将不胜感激任何帮助。 THANK YOU谢谢你

As the map file includes newline character at the end of lines, each line contains width + 1 characters.由于 map 文件在行尾包含换行符,因此每行包含width + 1字符。 Then the sequential fgetc() reads extra byte for each line and causes the skew in the vertical alignment.然后顺序fgetc()读取每一行的额外字节并导致垂直 alignment 中的偏斜。

A workaround will be to explicitly read the newline character such as:一种解决方法是显式读取换行符,例如:

for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
        ...
        map[row][col]=fgetc(mapfile);
    }
    fgetc(mapfile);                 // read the newline character and discard
}

Or more portable way:或更便携的方式:

char map[20][30];
char buf[BUFSIZ];                   // line buffer
mapfile = fopen(mapname, "r+");
for (int row = 0; row < height; row++) {
    fgets(buf, BUFSIZ, mapfile);    // read each line
    for (int col = 0; col < width; col++) {
        ...
        map[row][col]=buf[col];     // assign column values of the line
    }
}

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

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