ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
printf() is functioning just like it always does, the difference is your terminal settings. ncurses changes terminal settings to work the way it needs it to. printf() and ncurses are incompatible. Just don't do it unless you want to keep getting undefined results.
Single quotes need not to be escaped in C strings.
The issue is probably due to mixing two output buffers (the one used by printf and stdio functions and the one used by curses), although when I try your first program, the printf string is displayed on screen anyway ...
I bet it is a coincidence, replace the quote by another character, and it will be truncated at this new character.
The fact it is cut here is just I think due to an output buffer flush side effect.
#include <stdio.h>
#include <ncurses.h>
int main(){
initscr();
clear();
refresh();
printf("this will print\n"); // text I want printed
refresh();
getch(); //waits so it dosn't flash past too fast!
refresh();
endwin();
return 0;
}
Notice I added a refresh() after the printf(). NCURSES uses two buffers, the actual screen buffer (what we see), and work buffer (what we may want seen), the refresh connects the two; so anytime you output anything you must do some type of refresh to cause it to be seen.
If your trying to learn ncurses and need a slightly bigger example, then this link may help. It's my first ncurses program. Cassette Manager V4
technically yes; printw() replaces printf() in a ncurses program. but if you say that much you might as well tell the rest of the story... ie - there must be 20 ways to put something on the screen (and printf is not the best one). But you have to start somewhere.
The use of printf() puts things into the "unpredictable state". The nature of ncurses is to send a screen full with each refresh, which is why there is a work buffer to collect all screen writes until a refresh is issued to send it to the terminal. ncurses doesn't know we are using a local console - it thinks and treats it like a serial terminal.
Here is a rewrite of your test program which behave the way "I think you expected".
Code:
#include <stdio.h>
#include <ncurses.h>
int main(){
int x = 0;
stdscr = initscr();
cbreak(); /* read from input without waiting on end of line */
noecho(); /* Don't echo chars to screen on input */
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE); /* translate pf-keys into correct KEY_ values */
immedok(stdscr, TRUE); /* Automatic refresh ON */
clear();
refresh();
printw("this shouldn't print\n"); // text I want printed
getch(); //waits so it dosn't flash past too fast!
for ( x=0; x<100; x++)
{
printw("%d-this shouldn't print\n", x); // text I want printed
}
getch(); //waits so it dosn't flash past too fast!
refresh();
endwin();
return 0;
}
Notice what happens at the end of the screen or the last line of your terminal... You would have to create a window and learn how to scroll it, to have all 100 lines format as you expect.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.