-1
#include <curses.h>

int main(){
  initscr();
  refresh();
  
  char s[25]; 
  mvprintw(1,0,"Enter sentence: ");
  refresh();
  
  scanw("%s", s); // Input `one two`.
  mvprintw(2,0,"%s\n", s); // This just prints `one`.
  refresh();
  
  getch();
  endwin();
  return 0;
}

Input is one two.

Output is just one, second half is missing.

Does scanw not treat spaces properly?

Dan
  • 2,694
  • 1
  • 6
  • 19
  • 1
    "*Does scanw not treat spaces properly?*". It's working as designed. The specifiers mean the same as they do for `scanf`. From the [scanf manual](https://www.man7.org/linux/man-pages/man3/scanf.3.html) regarding `%s`: "Matches a sequence of non-white-space characters". That is, it stops matching on the first white space. So working exactly as intended. – kaylum Sep 11 '21 at 03:58

1 Answers1

1

scan functions generally breaks at whitespaces, so instead of

scanw("%s", s);

do

scanw("%24[^\n]", s); // always do bounds checking

That is, read at most 24 non-newlines.

And, you should always check that the extraction works:

if(scanw("%24[^\n]", s) == 1) { ... }

If you mix input from here and there, consider using the "greedy" version that "eats" leftovers:

scanw(" %24[^\n]", s)    // note the space at the beginning
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Your last character (the 25th one) would be the null terminating character correct? – Dan Sep 11 '21 at 04:09
  • @Dan Yes, and this works as expected _if_ the check (`if(... == 1`) is added. – Ted Lyngmo Sep 11 '21 at 04:11
  • doc says `returns ERR on failure and an integer equal to the number of fields scanned on success`, so `==1` check is if 1 field (or character) is scanned? – Dan Sep 11 '21 at 04:14
  • @Dan Yes, exactly that. If you do 2 scans, like `scanw("%d %d", &first_int, &second_int)`, the comparison would be `== 2` – Ted Lyngmo Sep 11 '21 at 04:26