LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-21-2008, 03:23 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
No Return to Command Line in C++


In my C++ programme, the main function calls a routine ShowGraphs, in which gnuplot is opened, plots some data to the screen, and then exits gnuplot when I press q (as usual in gnuplot). However, after the graphs disappear from the screen the programme does not return to the command line, but only if I terminate the programme with Ctrl+C or press s and then Enter, because I have inserted a most unappealing while loop:
Code:
int ShowGraphs()
{
 FILE *GnuPlot = popen("/usr/bin/gnuplot", "w");
 if(!GnuPlot)
  {
  cerr << "Not able to open GnuPlot: " << strerror(errno) << '\n';
  return 0;
  }

 (...)

 while(1)
 {
  if ('s' == getchar() )
  {
   pclose(GnuPlot);
   return 1;
  }
 }
}
If I do not insert the loop at the end of the ShowGraphs the programme executes correctly, but shows the graphs and removes them instantly from the screen, which is not what I want - I want to see them for a while. Is there a nice way (preferably without ncurses, if it's not too much of a fuss) to circumvent the problem and exit the main programme to the command line after I have watched the graphs for a while and pressed 'q', without then pressing s and then Enter? Just for the record, the ShowGraphs function is the last to be executed in the main function.
 
Old 02-21-2008, 12:23 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think "getchar()" already has exactly the functionality you're looking for (just wait, until the user types something in and hits <Enter> ... or, more simply, until the user just hits <Enter>...)

Try this:
Code:
int ShowGraphs()
{
  FILE *GnuPlot = popen("/usr/bin/gnuplot", "w");
  if(!GnuPlot)
  {
    cerr << "Not able to open GnuPlot: " << strerror(errno) << '\n';
    return 0;
  }

  (...)

  int c;
  while((c = getchar ()) != 'q')
    ;
  pclose(GnuPlot);
  return 1;
}
... or ...
Code:
  getchar ();
  pclose(GnuPlot);
'Hope that helps .. PSM
 
Old 02-22-2008, 07:24 AM   #3
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
I didn't think of the last one, which is actually very simple and neat, thanks. What I'd really like is that it quits the programme after the gnuplot graphs have been closed by 'q', but for the time being I'm happy with it.
 
  


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
Command getent return nothing aspenbr Linux - Software 1 11-18-2008 12:01 AM
return the username in a command matthewhardwick Linux - General 6 10-12-2007 06:30 AM
Remove New Line or Carriage return from Text File DIMonS Programming 13 10-01-2007 10:31 AM
xine command line? or some other command line only engine? lumix Linux - Software 2 08-10-2007 10:46 AM
Multi-line return from grep into an array? rose_bud4201 Programming 2 06-14-2005 04:11 PM

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

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