LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-23-2008, 04:59 AM   #1
sammoewel
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Question C advanced console printing


Hi all,

i'm writing a program and i would like to change predefined lines in the
printed text in the terminal, without changing the other text...

For example:
Code:
+----------------------------------------------------+
| Connected to 192.168.9.1                           |
|                                                    |
|                                                    |
|                                                    |
|                                                    |
| TEXT                                               |
| Status message                                     |
|                                                    |
+----------------------------------------------------+
Given this picture, i would like to change "TEXT" & "Status message", without changing the position of "Connected to ...".

Is the only way to do this messing with printf("\b"), or are there other
possibilities?

For example, how is the progress bar in ETA printed in the program 'wget' ?

Thanks in advance!

Sam
 
Old 10-23-2008, 05:53 AM   #2
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
For full screen console graphics, you need ncurses: http://www.gnu.org/software/ncurses/

But for simple wget like progress bars, all you need to do is print a carriage return character (a carriage return \r is like a newline \n except it doesn't go to a newline! you just return to the head of the current line, which is exactly what you want).

Code:
#include <stdio.h>
#include <unistd.h>
int main(){
    int i,j;
    for(i=1;i<=10;i++) {
         printf("%3d ",i);
         for(j=1;j<=i;j++) { 
             printf("=");
         }
         fflush(stdout); // need this because terminal normally buffers 
                         // until there is a newline printed.
         sleep(1);
         printf("\r"); // return to beginning of line
    }
    printf("\n");

    return 0;
}
 
Old 10-23-2008, 07:00 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Another possibility is using escape control sequences.
http://www.termsys.demon.co.uk/vtansi.htm

Most virtual terminals (xterm etc) have the capability to use VT100,VT200 escape sequences commands to control cursor movement as well as text Attributes.
 
Old 10-24-2008, 05:36 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by michaelk View Post
Another possibility is using escape control sequences.
http://www.termsys.demon.co.uk/vtansi.htm

Most virtual terminals (xterm etc) have the capability to use VT100,VT200 escape sequences commands to control cursor movement as well as text Attributes.
Which is what ncurses does, but saves you from dealing with the details of specific terminal types and capabilities.
--- rod
 
Old 10-25-2008, 05:14 AM   #5
sammoewel
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Ok, we're gonna try to do it using ncurses, thanks for the replies!
 
Old 10-25-2008, 06:31 AM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by sammoewel View Post
Ok, we're gonna try to do it using ncurses, thanks for the replies!
Search for ncurses color tutorial. There was a good tutorial somewhere that dealt exactly with terminal coloring. Unfortunatley I saw it quite some time ago and don't remember where it was.
 
  


Reply



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
Any advanced keyboard handling API for console? neoAKiRAz Programming 4 10-27-2007 12:39 AM
Printing with Toshiba Advanced Port Replicator pollycat Linux - Laptop and Netbook 0 12-06-2005 12:22 AM
Printing from the console? mentallo Linux - Software 2 12-02-2005 02:40 PM
Advanced Samba printing question bill_k_lopez Linux - Networking 1 11-09-2005 12:44 PM
printing on the console balu14u Programming 5 04-06-2005 05:35 AM

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

All times are GMT -5. The time now is 10:03 AM.

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
Open Source Consulting | Domain Registration