Solved

Does system("pause") work on Linux?

Posted on 2002-04-11
12
404 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now