Solved

Does system("pause") work on Linux?

Posted on 2002-04-11
12
408 Views
Last Modified: 2010-04-21
Does system("pause") work on Linux?

int main(int argc, char* argv[])
{
 printf("hello world");
 system("pause");
 return 0;
}

On DOS, the above code would pause, and wait for a keyboard hit, before exiting the program.

Does Linux have a similar pause command?
0
Comment
Question by:Axter
12 Comments
 
LVL 4

Expert Comment

by:newmang
ID: 6935566
Axter

Why do you want to do it this way?

Surely all you need is the following

int main(int argc, char * argv[])
{

  printf("hellow world\n");
  getchar();
  return 0;
}

Cheers - Gavin
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935570
Axter

Or the following to look more like DOS

int main(int argc, char * argv[])
{

 printf("hellow world\n");
 printf("Press any key to continue\n");
 getchar();
 return 0;
}

Cheers - Gavin
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935576
Oops - ignore those last two comments - getchar doesn't return until newline is pressed - sorry.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:newmang
ID: 6935606
Oops - ignore those last two comments - getchar doesn't return until newline is pressed - sorry.
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935731
OK - This works on my system.

Create a perl script

#!/usr/bin/perl
print "Press any key to continue\n";
system "stty cbreak </dev/tty >/dev/tty 2>&1";
$key = getc;
system "stty -cbreak </dev/tty >/dev/tty 2>&1";
print "\n";

Make this executable (chmod +x keypress.pl) and place it somewhere in the default path then call this in the system call in your C application.

Cheers - Gavin
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935742
More....

#!/usr/bin/perl
print "Press any key to continue\n";
system "stty cbreak </dev/tty >/dev/tty 2>&1";
system "stty -echo </dev/tty >/dev/tty 2>&1";
$key = getc;
system "stty -cbreak </dev/tty >/dev/tty 2>&1";
system "stty echo </dev/tty >/dev/tty 2>&1";
print "\n";

now it doesn't echo the character the user enters.
You could call this script pause, place it in the default path and use it in scripts as well as programs.

Cheers - Gavin
0
 
LVL 30

Author Comment

by:Axter
ID: 6935922
newmang,
I'm not looking for a replacement for system("pause").

I just want to know if it works in Linux.  That's all.

I'm an expert in the C++ topic area, and I post a lot of example code.
In VC++ it's easier to put system("pause") at the end of the main() function, so that the program doesn't end before being able to see the result of your test code.

I looking to see how portable is this method.

I don't have a Linux machine, so I can't test it myself.

Does Linux have a pause command?
0
 
LVL 4

Accepted Solution

by:
newmang earned 25 total points
ID: 6935944
NO
0
 
LVL 2

Expert Comment

by:fremsley
ID: 6937088
> so that the program doesn't end before being able to see
> the result of your test code.

You do not need to pause under Linux because in a terminal
window and on the text console you can always scroll back
using the Shift-PgUp/PgDn keys. To keep your code portable
you can test for predefined macros like:

#ifndef __unix__
  system("pause");
#endif

Hope it helps
0
 
LVL 30

Author Comment

by:Axter
ID: 6937113
fremsley,
Thanks for the info, but I was just looking for a yes or no answer, and I'm already familiar with compiler directive methods.
0
 

Expert Comment

by:Synthesist
ID: 8771646

Hello Axter,

I believe what your looking for is in the "wait" command. A simple "man wait" should fill in the details for you. ;-)

This may be a bit late but I was looking for that same command again. Hate it when my scripts exit before the other processes are done.
0
 
LVL 30

Author Comment

by:Axter
ID: 8772236
Synthesist,
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question