LinuxQuestions.org
Help answer threads with 0 replies.
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-14-2007, 11:04 PM   #1
vibrokatana
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Rep: Reputation: 15
C++ Output question


I always wondered how programs like wget and top create interfaces that can be updated without throwing out new lines of output.

I managed to dig out the function used to update the progress bar in wget:
Code:
static void display_image (char *buf)
{
  int old = log_set_save_context (0);
  logputs (LOG_VERBOSE, "\r");
  logputs (LOG_VERBOSE, buf);
  log_set_save_context (old);
}
My question is: how can you do the equivilent using strictly c++ stdlib or a library of some sort (I want to avoid using C and C++ outputs concurrently to avoid the performance issues).

Basically what I am trying to do is write a simple command line that may have text added above well after the prompt has been written to the terminal emulator. So far I have hit a pretty bad wall and have run out of ideas.

Any and all suggestions are welcome.
 
Old 10-15-2007, 12:03 AM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
While I'm not certain, I think they are using the terminfo library to do this, which is what ncurses is built on top of. Unfortunately, that's a pure C library and I think if you use it, you need to do so exclusively (I think). That is, if you use the terminfo/curses way of printing to the screen, you shouldn't mix in printf()'s or cout<<'s.
 
Old 10-15-2007, 12:15 AM   #3
vibrokatana
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
thx for the info. There seems to be C++ bindings for ncurses so I may look at using that. Nifty, if a bit more complicated then what I need :\
 
Old 10-15-2007, 10:26 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I think the wget function only changes stuff on the current line. If that's all you want, printing a carriage return ('\r') character will send the cursor back to the beginning of the line in most terminals. If you want to go above the current line you'll need ncurses or terminfo.
 
Old 10-15-2007, 10:34 AM   #5
vibrokatana
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
hmm, interesting bit. I may end up having to go with a ncurses type system. Does anyone know anything about terminfo, all the information I have looked up has basically described it just as a database explaining the capabilities of the terminal.

I wonder how hard it would be to derive something from cout or an existing output stream and see how hard it would be to write my own. I might beable to cache the existing text, erase it, output the new text, then place the text back. I guess I have some stuff to test.
 
Old 10-15-2007, 11:47 AM   #6
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Ntubski is right. Upon rereading the code snippet they are just using a carriage return. If one line isn't enough, I'd go with ncurses rather than try to use any raw terminfo, etc. It's just simpler.
 
Old 10-15-2007, 11:56 AM   #7
vibrokatana
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
doing a simple program in C++ you can't get the carriage return from what I can guess:

Code:
int main(int argc, char *argv[])
{
  cout << "Hello, world!" << endl;
  cout << endl;
  cout << "Hello again!" << endl;
  return EXIT_SUCCESS;
}
That will output as:
Code:
Hello, world!

Hello again!
So I would probably need to use c style outputs to interface that way.


I will probably put this project on a GUI, probably end up being easier and more robust to use without having to deal with abstract libraries or methods.
 
Old 10-15-2007, 12:09 PM   #8
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Well, the trouble is that you never tried using a carriage return. 'endl' translates to '\n' not '\r'. I'd recommend using '\r' explicitly.
 
Old 10-15-2007, 02:15 PM   #9
vibrokatana
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
hmm, weird behavior:
Code:
int main(int argc, char *argv[])
{
  cout << "Hello, world!" << '\r';
  cout << "Hello again" << endl;
  return EXIT_SUCCESS;
}
Code:
Hello againd!
Fun, I guess another work around is in order.

I will work on this later tonight, this should be easily solvable with the .size and .resize functions in the string class.

Code:
#include <iostream>
#include <string>

using namespace std;

string OverWriteFormat(string LastLine, string NewLine)
{
  int NewSize = NewLine.size();
  int LastSize = LastLine.size();

  if(NewSize < LastSize)
    NewLine.resize(LastSize, ' ');
  return NewLine;
}

int main(int argc, char *argv[])
{
  string LastString = "OMG WTF BBQ!";
  cout << LastString;
  cout << '\r' << OverWriteFormat(LastString, "OMG") << endl;
  cout << LastString << endl;
  return EXIT_SUCCESS;
}
That works fine, now all I have to worry about is how it will behave with an input :\

Last edited by vibrokatana; 10-15-2007 at 02:33 PM.
 
Old 10-15-2007, 05:27 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Outputing a string that is always the same length, adding blanks before the \r should help...
 
  


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
Grep output question Kvetch Programming 5 07-20-2007 02:59 AM
question about chrootkit's output Old_Fogie Linux - Security 7 08-01-2006 08:32 AM
Question about output for php Hockeyfan Programming 1 03-14-2006 09:30 AM
iwconfig output question scowles Linux - Wireless Networking 2 12-03-2004 05:11 PM
C Output Question drigz Programming 13 09-10-2004 07:32 AM

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

All times are GMT -5. The time now is 07:12 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