GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Aug-2006, 06:38
Catus Catus is offline
New Member
 
Join Date: Aug 2006
Location: Finland
Posts: 2
Catus is on a distinguished road

Misunderstanding curses refresh()?


I was under the impression that curses functions like printw() print to stdscr so you can build the whole "image" up and then to actually show it on the screen (That's curscr? At least sort of?) you need to refresh(). Like in the following program.

CPP / C++ / C Code:
#include <ncurses.h>

int main()
{
        initscr();
        printw("Hello World !!!");      /* Print Hello World              */
        refresh();
        getch(); 
        endwin();

        return 0;
}

But it just doesn't seem to matter whether I leave that refresh() in there or not. I've commented it out and nothing seems to change.

What makes me think this is a horrible and embarrasing misunderstanding are bits like this:


CPP / C++ / C Code:
#include <ncurses.h>

int main()
{	int ch;

	initscr();			/* Start curses mode 		*/
	raw();				/* Line buffering disabled	*/
	keypad(stdscr, TRUE);		/* We get F1, F2 etc..		*/
	noecho();			/* Don't echo() while we do getch */

    	printw("Type any character to see it in bold\n");
	ch = getch();			/* If raw() hadn't been called
					 * we have to press enter before it
					 * gets to the program 		*/

	printw("The pressed key is ");
	attron(A_BOLD);
	printw("%c", ch);
	attroff(A_BOLD);
	
	refresh();			/* Print it on to the real screen */
    	getch();			/* Wait for user input */
	endwin();			/* End curses mode		  */

	return 0;
}

In this example the user is obviously expected to see the request for pressing a key way before the refresh(). But on the other hand, the comment (These are both from tutorials I'm trying desperately to understand.) implies that it is indeed the refresh that makes it all visible.

I just don't understand how this works. Could someone please explain this matter in a way that even an obviously retarded person like me could understand it?
  #2  
Old 23-Aug-2006, 10:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Misunderstanding curses refresh()?


Quote:
Originally Posted by Catus
I was under the impression that curses functions like printw() print to stdscr so you can build the whole "image" up and then to actually show it on the screen
.
.
.
I just don't understand how this works.

I think that this is a pretty good question. I particularly appreciate that you included code examples (properly set off by code tags) and had specific questions about your problem. (It's also a pretty good question, but I said that already.)

The printw() function writes to an "imaginary" screen. It puts stuff into a buffer and updates some flags and does some other internal ncurses bookkeeping. It doesn't actually write anything to your real screen (the console window).

You can do as much printw() writing as you want to, but the stuff doesn't show up on the real screen until your program does something else to cause the "imaginary" screen buffer contents to go to the real screen.

One thing that causes the real screen to be updated from the printw() buffer is refresh() (as the tutorials all tell you).

Another thing that causes the real screen to be updated is a call to a console input function (like getch()). At least that is my observation for ncurses implementations on Linux (GNU gcc and ncurses library, or cygwin/gcc on my Windows XP platform). It seems that the tutorials don't always mention this.

But --- and this is important --- there may be programs where you do some printw() stuff and then go off and to something else (not console input). A common mistake is for people to get into the habit of not using refresh() and then wondering why their stuff sometimes doesn't get displayed properly.

Note that (if you have not set the terminal for "noecho"), characters entered in response to getch() are echoed immediately. They don't go to the "printw() buffer", and your program doesn't need refresh(). If you don't want the character to be echoed immediately, then use noecho() as the second program does. Then the character won't get to the real screen until you put it there (with printw() or some such thing).

If you are using Linux (or cygwin/gcc on Windows), you can try the following modification of your first example:

CPP / C++ / C Code:
#include <ncurses.h>
#include <unistd.h> /* for the sleep() function */
int main()
{
    initscr();                  /* Start curses mode */
    printw("Hello World !!!");  /* Print Hello World */
    printw("\nPress any key to continue... ");
/*  refresh(); */
    sleep(5);
    getch();                    /* Wait for user input */
    endwin();                   /* End curses mode */
    return 0;
}

Here is what I observed:

When I invoked the program, nothing happened on the screen for about five seconds, then the messages appeared.

Now, uncomment the refresh() and try it again. Here's what I observe:

With I invoked the program, the screen was cleared and the two messages appeared. If I press a key immediately, the program exits after about five seconds. If I wait for five or more seconds, the program exits as soon as I press the key.


Bottom line: using refresh() after printw() is like "taking" chicken soup for a cold: Might help; can't hurt.

Regards,

Dave
  #3  
Old 23-Aug-2006, 23:02
Catus Catus is offline
New Member
 
Join Date: Aug 2006
Location: Finland
Posts: 2
Catus is on a distinguished road

Re: Misunderstanding curses refresh()?


Ah, I see. Seems like I stumbled upon a bit of a newbie trap and got hit hard. (As evidenced by the fact that I forgot to mention my OS and compiler in the question like I intended. Not that it seems to matter but I figured that it wouldn't hurt either.) At least now I can put it all behind me and work on the interesting parts.

Thanks a lot for the great answer. I didn't really expect something so detailed and, well, long and I appreciate you taking the time to write all that. (I noticed that all your answers here seem to be long and detailed. I have no idea how you find the time and patience to write so much but I sure am glad you do.)
  #4  
Old 24-Aug-2006, 08:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Misunderstanding curses refresh()?


Quote:
Originally Posted by Catus
all your answers here seem to be long and detailed

"Sometimes I just can't help myself."
---davekw7x
On gidforums

"It's a gift ... and a curse."
---Adrian Monk
On the USA Network TV series Monk
  #5  
Old 14-Jun-2008, 05:23
oniris oniris is offline
New Member
 
Join Date: Jun 2008
Posts: 3
oniris is on a distinguished road

Re: Misunderstanding curses refresh()?


Hi guys, on this subject, I am actually worse off than Catus...

Let me first specify my specs:
OS: UBUNTU
IDE: Codeblocks 8.02 with G++ compiler

My problem is that I've been trying to learn Ncurses with a few different tutorials, but I've noticed that the function refresh() allways gives me an error. Example:

int main()
{
initscr();
printw("Hello World !!!"); /* Print Hello World */
refresh();
getch();
endwin();

return 0;
}

Results:

/usr/lib/libncurses.a(lib_mouse.o)||In function `_nc_mouse_init':|
(.text+0x833)||undefined reference to `dlopen'|
/usr/lib/libncurses.a(lib_mouse.o)||In function `_nc_mouse_init':|
(.text+0x84d)||undefined reference to `dlsym'|
/usr/lib/libncurses.a(lib_mouse.o)||In function `_nc_mouse_init':|
(.text+0x866)||undefined reference to `dlsym'|
/usr/lib/libncurses.a(lib_mouse.o)||In function `_nc_mouse_init':|
(.text+0x87f)||undefined reference to `dlsym'|
/usr/lib/libncurses.a(lib_mouse.o)||In function `_nc_mouse_init':|
(.text+0x89||undefined reference to `dlsym'|
/usr/lib/libncurses.a(lib_mouse.o)||In function `_nc_mouse_init':|
(.text+0x8b5)||undefined reference to `dlclose'|
||=== Build finished: 6 errors, 0 warnings ===|


On the other hand if I leave refresh out of the story, example:

#include <ncurses.h>

int main()
{
initscr();
printw("Hello World !!!"); /* Print Hello World */
getch();
endwin();

return 0;
}

Results:

Compiling: /home/ysilk/Desktop/Ze.cpp
Linking console executable: /home/ysilk/Desktop/Ze
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

Checking for existence: /home/ysilk/Desktop/Ze
Executing: xterm -T '/home/ysilk/Desktop/Ze' -e /usr/bin/cb_console_runner "/home/ysilk/Desktop/Ze" (in /home/ysilk/Desktop)
Process terminated with status 0 (0 minutes, 1 seconds)

Any ideas

PS: Sorry to have necroed this 2 y old topic but it seemed relevant
  #6  
Old 14-Jun-2008, 09:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Misunderstanding curses refresh()?


Quote:
Originally Posted by oniris
...OS: UBUNTU
IDE: Codeblocks 8.02 with G++ compiler

I don't use codeblocks, so I can't help you with anything related to it

Here's my suggestion:

I strongly recommend that you start with command-line compilations of simple programs to make sure you know what compiler/linker options to use. You will see exactly what the compiler is telling you without messages being filtered through some so-called "helpful" gui IDE interface.

So, my first recommendation is to use command line stuff to try your example. (Later you can try a Makefile or go on to codeblocks.)

Secondly, since your example is a valid C program, why not start with compiling it as a C program? Get into C++ as soon as you want to, but start with C.

So, save your program as something like test.c:

CPP / C++ / C Code:
#include <ncurses.h>

int main()
{
    initscr();
    printw("Hello Ncurses World !!!\n\n");
    printw("Press any key to continue . . . ");
    refresh();
    getch();
    endwin();

    return 0;
}

Then compile from a command line as follows:

Code:
gcc -Wall -W -pedantic test.c -o test -lncurses

With my Ubuntu 8.04 and GNU version 4.2.3, there were no compiler messages of any kind. See Footnote

When I executed the program with
Code:
./test
I saw the messages from the printw statements.

How did yours go?

Now, copy or rename the file to be named "test.cpp" and compile

Code:
g++ -Wall -W -pedantic test.cpp -o test -lncurses

The results should be the same: No compiler messages of any kind, and execution has the expected results.

As I mentioned in previous posts on this thread, the "refresh()" statement is not needed here, but it should compile OK. (As we basketball fans would say: "No harm---no foul.").
Quote:
Originally Posted by oniris
Sorry to have necroed this 2 y old topic but it seemed relevant
It is entirely appropriate to ask a question about the same topic covered in a previous thread.

Regards,

Dave

Footnote:

I am assuming that you have the ncurses stuff installed on your system, otherwise I don't think it would compile---with or without the refresh() stuff.

In the System->Administration->Synaptic Package Manager application, do a search for ncurses. On my system I see that there are a number of things installed. You should have at least the following:

ncurses-base
ncurses-bin
libncurses5
libncurses5-dev

(The little boxes are green.)
  #7  
Old 14-Jun-2008, 10:40
oniris oniris is offline
New Member
 
Join Date: Jun 2008
Posts: 3
oniris is on a distinguished road

Re: Misunderstanding curses refresh()?


Thanks for the help davekw7x, that was a very helpful post

Unfortunately, one problem remains... But let me give you some details:

Your first advice was to switch to the command line compiler and to try compiling it with a .c extension. That I did and it successfully compiled and ran.

When I switched the extension to .cpp though, it failed and gave me the following verbose:
ysilk@ysilk-desktop:~/Desktop$ gcc -Wall -W -pedantic Ze.cpp -o Ze -lncurses
/tmp/ccmhO01W.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status.

One more question, how do you set up that nice layout for the source code?

Regards.
  #8  
Old 14-Jun-2008, 11:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Misunderstanding curses refresh()?


Quote:
Originally Posted by oniris
... it successfully compiled and ran.
Huzzah!
Quote:
Originally Posted by oniris
When I switched the extension to .cpp though, it failed

Go to the Synaptics Package Manager (System->Administration->Synaptics Package Manger). Do a search for g++. Assuming that your Ubuntu is fairly recent and is a 32-bit system, the following (at least) should be checked (green boxes):
g++
g++-4.2
libstdc++6
libstdc++6.4.2
libstdc++6.4.2-dev

Ubuntu apparently doesn't automatically install the c++ stuff unless you ask for it. (I think it just installs a minimal amount of C stuff to recompile the Kernel, but not enough for "real" development.)

Anyhow, if they are not checked, check them and install.

Try the following test program:

CPP / C++ / C Code:
//hi.cpp
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello C++ World!" << endl;
    return 0;
}

Compile with something like

Code:
g++ hi.cpp -Wall -W -pedantic -o hi

There should be no compiler messages of any kind.

Then execute
Code:
./hi
Let us know how it works out!


Anyhow, that's why I like to start with something "simple" that has a high probability of working and then move up to more "interesting" stuff.


Quote:
Originally Posted by oniris
One more question, how do you set up that nice layout for the source code?
If you mean how do I get it to display nicely on the forum, then you can do the following:

Paste your source code into the submit window.

Highlight it.

On the right-hand end of the set of icons directly above submit window, there is "C++". When you move your mouse cursor over that, it shows "Wrap with CPP".

Click it and the highlighted text will be surrounded by [cpp] and [/cpp] code tags.

Or, just paste the code into the submit window.
Put [cpp] before the first line of code.

Put [/cpp] after the last line of code.

Click the "Preview Post" button before submitting, and you can see how it will appear on the forum.

Regards,

Dave

Footnote: If you want stuff to appear in the forum exactly as you see it on your terminal (like compiler messages, not C or C++ source code) then you can surround such text with [code] and [/code] code tags. The "#" icon does this for highlighted text, or you can simply put the tags before and after your text.
  #9  
Old 14-Jun-2008, 14:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Misunderstanding curses refresh()?


Quote:
Originally Posted by oniris
When I switched the extension to .cpp though, it failed and gave me the following verbose:
Code:
ysilk@ysilk-desktop:~/Desktop$ gcc -Wall -W -pedantic Ze.cpp -o Ze -lncurses /tmp/ccmhO01W.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status.
Hmmm. I didn't notice your command line before I posted all of the stuff about seeing whether the g++ stuff is installed on your system.

The command for compiling and linking C++ files is g++, as I showed in my example, not gcc. So try it again with g++ and see if it works. If it still won't go, then look at the package manager, as I suggested, to see if the g++ libraries are there.

Here's the thing: For the GNU compiler suite, gcc and g++ are actually the same compiler (different rules are enforced for the different source types, but the compiler itself is the same). With no other command line arguments, the compiler treats a file with a ".c" extensiion as a C file and if it has a ".cpp" extension it's a C++ file. So, gcc can actually compile a C++ file. The problem is at link time: If you use gcc on a .cpp file, it may very well be compiled OK, but the gcc command line doesn't automatically link in the C++ run-time library stuff.

Bottom line(s):

For anything.c use gcc anything.c ...
For anything.cpp use g++ anything.cpp ...

Regards,

Dave
  #10  
Old 17-Jun-2008, 05:50
oniris oniris is offline
New Member
 
Join Date: Jun 2008
Posts: 3
oniris is on a distinguished road

Great Help


Thanks for everything Dave, I was indeed missing one of g++ development files and I was trying to compile it with gcc. You solved my problem and added to my knowledge in the process...

Chapeau bas,

Oniris
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a curses display - question. smoothdogg00 C++ Forum 18 16-Apr-2006 13:59
IE 7 Beta 2 Refresh Install Problem MaXimus Computer Software Forum - Windows 2 22-Mar-2006 03:53
decoding incomming data and screen refresh avmtptortri C Programming Language 5 19-Dec-2005 09:18
Persistant New Posts without Refresh cable_guy_67 GIDForums™ 1 22-Nov-2005 09:31
How to refresh a widget immediately dsmith FLTK Forum 8 11-Dec-2004 01:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.