1

I am trying to print ascii art, which is stored in a separate file, to the terminal using ncurses.

Here is my C code:

#include <ncurses.h>
#include <string.h>

int main() {
  initscr();
  raw();

  WINDOW * startScreen = newwin(20, 70, 0, 0);
  curs_set(FALSE);
  start_color();
  init_pair(1, COLOR_WHITE, COLOR_BLACK);
  attron(COLOR_PAIR(1));
  /** (READS AND PRINTS THE FILE HERE, DESCRIPTION BELOW) **/
  wrefresh(startScreen);
  attroff(COLOR_PAIR(1));
  wgetch(startScreen);
  endwin();
}

The above code reads the content of a file and print a block character if the character is an # in the file. (sort of printing the file character by character) Everything is printed in startScreen, an independent window.

Here's the content of the the file:

##     ##  ######   ######   ######  ######  ##        ########
###   ###    ##    ##    ## ##    ##   ##    ##        ##
#########    ##    ##       ##         ##    ##        ##
## ### ##    ##     ######   ######    ##    ##        ######
##  #  ##    ##          ##       ##   ##    ##        ##
##     ##    ##    ##    ## ##    ##   ##    ##        ##
##     ##  ######   ######   ######  ######  ########  ########


  #####   ######  ##     ## ##     ##   ###    ##   ## ######
 ##   ## ##    ## ###   ### ###   ###  ## ##   ###  ## ##   ##
##       ##    ## #### #### #### #### ##   ##  #### ## ##    ##
##       ##    ## ## ### ## ## ### ## ##    ## ####### ##    ##
##       ##    ## ##  #  ## ##  #  ## ######## ## #### ##    ##
 ##   ## ##    ## ##     ## ##     ## ##    ## ##  ### ##   ##
  #####   ######  ##     ## ##     ## ##    ## ##   ## ######

However, when I run the C code, it gives this:

▒▒     ▒▒  ▒▒▒▒▒▒   ▒▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒▒▒▒▒  ▒▒        ▒
▒▒▒   ▒▒▒    ▒▒    ▒▒    ▒▒ ▒▒    ▒▒   ▒▒    ▒▒        ▒▒
▒    ▒▒    ▒▒               ▒▒         ▒▒    ▒▒        ▒▒
▒▒ ▒▒▒ ▒▒    ▒▒     ▒▒▒▒▒▒   ▒▒▒▒▒▒    ▒▒    ▒▒        ▒▒▒▒▒▒
▒▒  ▒  ▒▒    ▒▒          ▒▒       ▒▒   ▒▒    ▒▒        ▒▒
▒▒     ▒▒    ▒▒    ▒▒    ▒▒ ▒▒    ▒▒   ▒▒    ▒▒        ▒▒
▒▒     ▒▒  ▒▒▒▒▒▒   ▒▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒▒▒▒▒  ▒  ▒


  ▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒     ▒▒ ▒▒     ▒▒   ▒▒▒    ▒▒   ▒▒ ▒▒▒▒▒▒
 ▒▒   ▒▒ ▒▒    ▒▒ ▒▒▒   ▒▒▒ ▒▒▒   ▒▒▒  ▒▒ ▒▒   ▒▒▒  ▒▒ ▒▒   ▒▒
▒▒       ▒▒    ▒▒ ▒▒▒▒ ▒▒▒▒ ▒▒▒▒ ▒▒▒▒ ▒▒   ▒▒  ▒▒▒▒ ▒▒ ▒▒    ▒▒
▒▒       ▒▒    ▒▒ ▒▒ ▒▒▒ ▒▒ ▒▒ ▒▒▒ ▒▒ ▒▒    ▒▒ ▒ ▒▒    ▒▒
▒▒       ▒▒    ▒▒ ▒▒  ▒  ▒▒ ▒▒  ▒  ▒▒ ▒ ▒▒ ▒▒▒▒ ▒▒    ▒▒
 ▒▒   ▒▒ ▒▒    ▒▒ ▒▒     ▒▒ ▒▒     ▒▒ ▒▒    ▒▒ ▒▒  ▒▒▒ ▒▒   ▒▒
  ▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒     ▒▒ ▒▒     ▒▒ ▒▒    ▒▒ ▒▒   ▒▒ ▒▒▒▒▒▒

I have completely no idea why are there blocks missing. Why is it behaving like this?

There's another question with a similar cause. However, the characteristic of the problem is different, thus a separate question.

gabrielchl
  • 606
  • 9
  • 20
  • Hm, this should work I think. Did you try the command "reset" to reset your terminal to sane defaults? Maybe it is messed up somehow. – Ctx Apr 01 '19 at 11:01
  • Which OS is this running on - Windows, Linux, MACOs etc – cup Apr 01 '19 at 11:01
  • @Ctx tried that, doesn't work – gabrielchl Apr 01 '19 at 11:04
  • @cup I'm running it in Ubuntu in Windows, using Windows Subsystem for Linux – gabrielchl Apr 01 '19 at 11:05
  • Aside: have you tried `fgetc` instead of `getc`? – Weather Vane Apr 01 '19 at 11:12
  • @WeatherVane make no difference :( I don't think the problem is with the file reading part, I can `printf` the contents character by character normally – gabrielchl Apr 01 '19 at 11:13
  • 1
    @GabrielLee Maybe your graphics file is messed up subtly; maybe try copy/pasting the content from above into a fresh textfile. – Ctx Apr 01 '19 at 11:13
  • 1
    Note that [`getc`](https://en.cppreference.com/w/c/io/getc) (as well as [`fgetc`](https://en.cppreference.com/w/c/io/fgetc)) return an `int`. That is actually very important for the comparison to `EOF`. – Some programmer dude Apr 01 '19 at 11:14
  • @Ctx no differrence :( – gabrielchl Apr 01 '19 at 11:15
  • 2
    Still, I suggest putting another `else if` in the code for the space character and then `else` to trap any other characters. – Weather Vane Apr 01 '19 at 11:18
  • 1
    @GabrielLee Out of curiosity, I copied/compiled your code as well as the ascii art, it works without problems here. Must have something to do with your environment. Do you by chance transmit the data via serial or another insecure channel? – Ctx Apr 01 '19 at 11:22
  • @WeatherVane yeah, you're right. I've changed my code to print a `X` if it's an unexpected character, but the problem still exists – gabrielchl Apr 01 '19 at 11:22
  • @Ctx maybe I should switch to gentoo and try it again – gabrielchl Apr 01 '19 at 11:23
  • @Ctx Yes! it works in gentoo linux. maybe I should leave this question open until someone knows why it somehow doesn't work in windows subsystem for linux – gabrielchl Apr 01 '19 at 11:28
  • 1
    @GabrielLee Ok, good. But I fear, if _you_ don't investigate that, find it out and tell us here, noone will ;) It might be a timing issue, you could start with adding a small delay between printing the characters – Ctx Apr 01 '19 at 11:30
  • 1
    @Ctx I'm doing different test cases now, wait a moment – gabrielchl Apr 01 '19 at 11:36
  • @Ctx yes, it seems to be working in ubuntu in windows if there's a short sleep between each character print, what can I do to investigate deeper? – gabrielchl Apr 01 '19 at 11:51
  • @GabrielLee My experience with this environment is unfortunately very limited (zero), so it gets hard for me here. Nevertheless, the information is interesting, so it might be worth that you collect the information in an own answer to your question (at least I would vote that up) – Ctx Apr 01 '19 at 11:54
  • @Ctx ya, maybe i'll do that later, it's strange tho – gabrielchl Apr 01 '19 at 11:55
  • @Ctx I just posted the ans, seems that it's not related to timing, but refresh, i'm not sure why tho – gabrielchl Apr 01 '19 at 12:59
  • Possible duplicate of [NCurses with WSL Displaying Boxes Incorrectly](https://stackoverflow.com/questions/55145136/ncurses-with-wsl-displaying-boxes-incorrectly) – Thomas Dickey Apr 01 '19 at 21:29

1 Answers1

1

It seems that this problem is unique in Windows Subsystem for Linux, where I first came across this problem.

The same script works 100% fine in Linux, but not in Windows Subsystem for Linux.

A workaround to make it work in WSL is to refresh the window after printing each character, wrefresh(startScreen). However, the root cause is still unknown.

gabrielchl
  • 606
  • 9
  • 20