LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C (ncurses) printing blank characters (https://www.linuxquestions.org/questions/programming-9/c-ncurses-printing-blank-characters-764345/)

cast_kostas 10-25-2009 11:50 AM

C (ncurses) printing blank characters
 
Hi all,
First of all i'd like to say that I'm new to linux (I use Slackware 13) and it's the first time i use the ncurses library.

Concerning my problem, i am trying to make a program with ncurses that prints a labyrinth and user moves a little robot out of it. I have the labyrinth in a txt file called map.txt and i copy this to a 2D 60x20 array called map. I printed the array and seems ok. The problem is that when i am trying to print the array after creating a window (printw("%c",map[i][j]) the cursor moves as if the program has printed the contents but there are only blank chars on the screen.

I read in another thread on this forum (http://www.linuxquestions.org/questi...-prob.-346656/) that this could be happening because there are \r\n chars in the txt file. But even if that's the problem i don't know how to remove them.

Here is the code:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>


int main(int argc, char **argv){
 
  FILE *fptr;
  char ch,map[62][20],ex;
  int i,j;
  int X,Y;//column=20; row=8;
  int mv;



  if( (fptr=fopen("map.txt", "r"))==NULL)
  {
    printf("map.txt doesn't exist.\n");
    exit(1);
  }

  i=0;
  j=0;
  while((ch=getc(fptr))!= EOF){
    map[i][j]=ch;
    j++;
    if(j>19){
      i++;
      j=0;}
  }

  fclose(fptr);

  for(i=0;i<62;i++)
    for(j=0;j<20;j++)
  printf("%c",map[i][j]);

  if(initscr()==NULL)
    printf("Error initializing ncurses!\n");
  cbreak();
  noecho();
  keypad(stdscr,TRUE);
  do{
  for(i=0;i<62;i++)
    for(j=0;j<20;j++)
    printw("%c",map[i][j]);
  /*j++;
  if(j>19)
  i++;j=0;}*/
  refresh();
  ex=getch();}
  while (ex!='q');
  endwin();

  return 0;
}

And here is the labyrinth that i'm trying to print on the window:
Code:

************************************************************
*                                                          *
* ********* **** ************ ****************** ********* *
* ********* **** ************ *        ********* ********* *
* ********* **** ************ * ****** ********* ********* *
* ********* ****            * *        ********* ********* *
* ********* ***************** ******** ********* ********* *
*          *                                  *          *
* ******************* * *** * **************************** *
* ******************* * *** * **************************** *
* ******************* * *** * **                        ** *
* ******************* * *** * ************ *************** *
*          ********* *    * ************ *****          *
* **** **** ********* ******* ************ ***** **** **** *
* **** **** *********        ************ ***** **** **** *
*          ****************************** ***** **** **** *
* **** **** *                                  * ****  ** *
* **** **** ***************** ****************** ********* *
*                                                          E
************************************************************


Any help or idea appreciated. Thank you!

PS:It's actually somewhat urgent(!!). School project. I'd be grateful if anyone could help!

rjlee 10-25-2009 02:30 PM

You probably want to look at the dos2unix utility.

cast_kostas 10-25-2009 08:31 PM

Quote:

Originally Posted by rjlee (Post 3732058)
You probably want to look at the dos2unix utility.

Hmm, what am i supposed to do with that? I don't really know how it works (after googling a bit i found out it's called fromdos on Slackware. I made a new file on linux copying the labyrinth there..But i got the same result..


All times are GMT -5. The time now is 03:20 AM.