Solved

Does system("pause") work on Linux?

Posted on 2002-04-11
12
403 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
Comment Utility
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
Comment Utility
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
Comment Utility
Oops - ignore those last two comments - getchar doesn't return until newline is pressed - sorry.
0
 
LVL 4

Expert Comment

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

Expert Comment

by:newmang
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Author Comment

by:Axter
Comment Utility
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
Comment Utility
NO
0
 
LVL 2

Expert Comment

by:fremsley
Comment Utility
> 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
Comment Utility
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
Comment Utility

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
Comment Utility
Synthesist,
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

13 Experts available now in Live!

Get 1:1 Help Now