LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2004, 06:26 PM   #1
jimsproch
Member
 
Registered: Oct 2004
Posts: 38

Rep: Reputation: 15
Can ANYONE explain this?


original post removed

Last edited by jimsproch; 08-09-2010 at 06:44 PM.
 
Old 12-31-2004, 07:13 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 01-01-2005, 03:52 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Looks like your line
Code:
printf("this shouldn't print\n");
is being interpreted wrong due to the unescaped '. I'd just leave it if the results of mixing ncurses and printf are undefined.
 
Old 01-01-2005, 04:52 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You don't need to escape ' in a string. You only need to escape it as a single character representation such as char ch = '\\'';
 
Old 01-01-2005, 05:00 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,703

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
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 ...
 
Old 01-01-2005, 05:43 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
I was refering to
Quote:
3. Where did the 't go on the last line of program two?
as it seemed more than coincidence that the string was truncated at the '.
 
Old 01-01-2005, 09:23 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,703

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
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.
 
Old 01-02-2005, 09:23 PM   #8
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
Add a refresh after changing the screen.

Code:
#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

Hope this helps

Last edited by skoona; 01-02-2005 at 09:56 PM.
 
Old 01-02-2005, 09:30 PM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
aren't you supposed to be using printw in a curses program?
 
Old 01-02-2005, 10:03 PM   #10
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
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.

Last edited by skoona; 01-02-2005 at 10:08 PM.
 
Old 01-03-2005, 12:38 AM   #11
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
The reason I asked is that I'm guessing it would exhibit more predictable behavior with refresh().. haven't tried it though
 
Old 01-03-2005, 06:49 AM   #12
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
Some more of the story.

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.

Last edited by skoona; 01-05-2005 at 07:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can somebody explain this to me? SyBEX Linux - Software 3 10-24-2004 05:32 PM
Can someone explain me what this is? hemmelig Linux - Software 3 10-14-2004 06:16 PM
can someone explain this? SteveGodfrey Linux - Wireless Networking 1 05-28-2004 08:47 AM
please someone, do explain this to me... Tisch Slackware 4 01-17-2004 10:37 AM
Can someone explain this ? beyond_2000 Linux - Hardware 6 10-08-2002 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration