Solved

Does system("pause") work on Linux?

Posted on 2002-04-11
12
406 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LDAP server on Linux Redhat 6 2 509
understanding an rpm spec file 5 66
Problem to run file 9 126
Set-Top-Box hardware and Open Source! 8 256
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 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…

808 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