- Thread: C++: Equivalent of system(«pause»); from Windows in Linux
- C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- Re: C++: Equivalent of system(«pause»); from Windows in Linux
- system(«PAUSE») for linux?
- system() in C/C++
Thread: C++: Equivalent of system(«pause»); from Windows in Linux
Thread Tools
Display
C++: Equivalent of system(«pause»); from Windows in Linux
What I want to do is pause program execution in the middle of a program and require the user to press a key to continue execution of the program.
Is there a way to do that with Linux?
Re: C++: Equivalent of system(«pause»); from Windows in Linux
Something like this should do the trick:
int main ( int argc , char * argv [])
<
std :: cout «Press enter to continue . » ;
std :: cin . get ();
Re: C++: Equivalent of system(«pause»); from Windows in Linux
As a general rule, try not to use the system() function in production code. If it’s for yourself, that’s fine. But to the general public, it presents a security risk and generally makes the program OS-specific.
cabalas’s suggestion is a sane one.
Re: C++: Equivalent of system(«pause»); from Windows in Linux
i normally just throw getch() at the end of my main function
Re: C++: Equivalent of system(«pause»); from Windows in Linux
The problem with that is that cin.get() isn’t actually waiting for input.
It seemingly does nothing.
Last edited by camper365; May 2nd, 2009 at 10:13 PM .
Re: C++: Equivalent of system(«pause»); from Windows in Linux
That’s probably because you took in input at an earlier stage of your app, and a newline is still sitting in the input stream. cin.get() is happy to gobble it, without actually waiting for you to enter anything.
To clear the input stream, try something like:
Re: C++: Equivalent of system(«pause»); from Windows in Linux
getch() is platform-dependent, and therefore a generally bad idea.
Re: C++: Equivalent of system(«pause»); from Windows in Linux
I figured that out.
Re: C++: Equivalent of system(«pause»); from Windows in Linux
as mentioned above, there is still a newline in the stream, so i usually use one of these two methods:
cin.get(); //the first get uses up the newline character
cin.get(); // with the stream now empty, this one actually waits for a character
cin.ignore(); // ignores the newline
cin.get(); //waits for character
essentially the same thing, but the latter is more readeable, and logically makes more sense
Re: C++: Equivalent of system(«pause»); from Windows in Linux
as mentioned above, there is still a newline in the stream, so i usually use one of these two methods:
cin.get(); //the first get uses up the newline character
cin.get(); // with the stream now empty, this one actually waits for a character
cin.ignore(); // ignores the newline
cin.get(); //waits for character
essentially the same thing, but the latter is more readeable, and logically makes more sense
You brought a thread back to life that has been dormant for more than 3 years! And to boot, your suggestion above is incomplete. There are cases where a user might input more than just a single character and a newline, regardless of what the prompt instructs them to do.
Last edited by dwhitney67; June 24th, 2012 at 01:31 PM .
Источник
system(«PAUSE») for linux?
In article , Paminu wrote:
:Is there something like system(«PAUSE») for linux?
As far as this newsgroup is concerned, the exact replacement is
That’s because as far as this newsgroup is concerned, PAUSE has
an unidentified system-dependant function. If the functionality of
PAUSE were to be described, then a Unix or Linux newsgroup would be
able to tell you what the nearest Linux equivilent would be.
On some systems, PAUSE means «suspend CPU activity on the system until
there is an interrupt». On others, it means «suspend the active process
for 1 second». On others, it means «flush any pending output and
wait until a key is pressed and then continue (without waiting for
a newline.) On others, the newline is required. On others, it means
«print the error message associated with not finding a named program
or command». On others it means «look for an executable program
named PAUSE in the current directory and execute it, with whatever
consequences that has.» Others yet it would mean «look for an
executable program named PAUSE in the currently defined list of
program locations, and execute it, with whatever condequences that has.»
Possibly the most common meaning, though, is «Play a recording
of your voice telling the dog to get his paws off of the furniture.»
If that’s the one you were looking for, see section 93.11 of the
Linux FAQ, which discusses the various ways of getting high-fidelity
playback of His Master’s Voice on various kinds of sound cards
and Midi Synthesizers.
—
All is vanity. — Ecclesiastes
Is there something like system(«PAUSE») for linux?
We don’t know. Try a Linux or Unix group. comp.unix.programmer is a
likely place to ask about this — but first explain what
system(«PAUSE») means.
But first, try reading the documentation that comes with the system.
—
Keith Thompson (The_Other_Keith) ks***@mib.org
San Diego Supercomputer Center
We must do something. This is something. Therefore, we must do this.
Is there something like system(«PAUSE») for linux?
This is off-topic for comp.lang.c, you should have posted to
comp.unix.programmer. However, I have written a clone of the MS-DOS
pause command for Linux, and I can give you a link to it here.
It should work on other Unix-like systems too. Extract it, change to the
directory pause-1.0, and type
make
to build, and you can type
sudo make install
to copy the executable to /usr/local/bin
Is there something like system(«PAUSE») for linux?
Linux programs generally don’t do that. It makes it hard
to use your program in a pipe context, plus one usually
runs command line programs. from the command line. so by
default there’s no danger of the window just disappearing
as soon as the program is done.
It’s usually left to the user to pipe your program’s output
through «less» or «more» if they want to pause between
screenfuls of data.
Is there something like system(«PAUSE») for linux?
This is off-topic for comp.lang.c, you should have posted to
comp.unix.programmer. However, I have written a clone of the MS-DOS
pause command for Linux, and I can give you a link to it here.
It should work on other Unix-like systems too. Extract it, change to the
directory pause-1.0, and type
make
to build, and you can type
sudo make install
to copy the executable to /usr/local/bin
Is there something like system(«PAUSE») for linux?
Assuming that system(«PAUSE») causes a Windows box to display «press any
key to continue», and then waits for any keystroke, the answer is «no».
(Check the FAQ. I’m sure someone can give the exact section number.)
However, if you are willing to go for «press enter to continue» instead,
then you can simply printf() that message, and use a while loop waiting
for getchar() to return ‘\n’.
Is there something like system(«PAUSE») for linux?
This is off-topic for comp.lang.c, you should have posted to
comp.unix.programmer. However, I have written a clone of the MS-DOS
pause command for Linux, and I can give you a link to it here.
It should work on other Unix-like systems too. Extract it, change to the
directory pause-1.0, and type
make
to build, and you can type
sudo make install
to copy the executable to /usr/local/bin
Ok done that but how do I use it in my source.c file??
If it’s installed to a location that is in your PATH, then you need only
write system(«pause»); in your source code. Note that Linux is
case-sensitive. It won’t work if you use uppercase «PAUSE» in your
program but the program is called lowercase «pause».
You might want to create a directory under your home directory to store
your own commands, such as
Then, add a line to your
/.profile file to make sure that this
directory is always added to your path:
export PATH=$PATH:$HOME/bin
Log out and back in, and check that the directory was added to your
path. You can check your path list by typing
echo $PATH
Is there something like system(«PAUSE») for linux?
You don’t want to do that.
While it’s certainly possible, you
1) Don’t want to use system(3) just to pause the program: create a
pause() function. «man 3 termios» to disable line buffering, or
check out [n]curses.
2) Need to ask yourself why you need this. If you are running on a
TTY or PTY, what is the benefit?
The only use I’ve seen for this in Windows programs is to work around
the horrible broken terminal emulator that comes with Windows. You
won’t have that problem on Linux: the emulator is not going to close
when your program terminates, so this is pointless.
Regards,
Roger
—
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
The only use I’ve seen for this in Windows programs is to work around
the horrible broken terminal emulator that comes with Windows.
Actually, I use the «pause» functionality (built-in in DOS/Windows; written
by me [and others] for Unix) frequently on both platforms for script
debugging.
And note, BTW, that some scripts are never (fully) debugged because the
data sources are unreliable. So, the pauses must be left in in perpetuity.
You won’t have that problem on Linux: the emulator is not going to close
when your program terminates, so this is pointless.
(It is somewhat unclear to what you are really referring here, since Windows
doesn’t really have an «emulator» — in the sense that Unix does. But,
leaving that aside for the moment. )
Actually, this *is* a problem under Unix, as frequent postings in the shell
group(s) attest. That is, people looking for something along the lines of:
xterm -e sh -c ‘. ;echo «Press enter. «;read x’
While it’s certainly possible, you
1) Don’t want to use system(3) just to pause the program: create a
pause() function.
Why is it a bad idea to use system() to pause a program? Waiting
for a keystroke from a user is hardly performance-intensive.
By the way, please don’t use Unix manpage notation in
comp.lang.c. It confuses people who are not familiar with Unix,
because it looks like a function invocation.
—
«It would be a much better example of undefined behavior
if the behavior were undefined.»
—Michael Rubenstein
While it’s certainly possible, you
1) Don’t want to use system(3) just to pause the program: create a
pause() function.
Why is it a bad idea to use system() to pause a program? Waiting
for a keystroke from a user is hardly performance-intensive.
Источник
system() in C/C++
system() is used to invoke an operating system command from a C/C++ program.
Note: stdlib.h or cstdlib needs to be included to call system.
Using system(), we can execute any command that can run on terminal if operating system allows. For example, we can call system(“dir”) on Windows and system(“ls”) to list contents of a directory.
Writing a C/C++ program that compiles and runs other program?
We can invoke gcc from our program using system(). See below code written for Linux. We can easily change code to run on windows.
system() vs using library functions:
Some common uses of system() in Windows OS are, system(“pause”) which is used to execute pause command and make the screen/terminal wait for a key press, and system(“cls”) which is used to make the screen/terminal clear.
However, making a call to system command should be avoided due to the following reasons:
- It’s a very expensive and resource heavy function call
- It’s not portable: Using system() makes the program very non-portable i.e. this works only on systems that have the pause command at the system level, like DOS or Windows. But not Linux, MAC OSX and most others.
Let us take a simple C++ code to output Hello World using system(“pause”):
The output of the above program in Windows OS:
This program is OS dependent and uses following heavy steps.
- It suspends your program and simultaneously calls the operating system to opens the operating system shell.
- The OS finds the pause and allocate the memory to execute the command.
- It then deallocate the memory, exit the Operating System and resumes the program.
Instead of using the system(“pause”), we can also use the functions that are defined natively in C/C++.
Let us take a simple example to output Hello World with cin.get():
The output of the program is :
Thus, we see that, both system(“pause”) and cin.get() are actually performing a wait for a key to be pressed, but, cin.get() is not OS dependent and neither it follows the above mentioned steps to pause the program.
Similarly, in C language, getchar() can be used to pause the program without printing the message “Press any key to continue…”.
A common way to check if we can run commands using system() in an OS?
If we pass null pointer in place of string for command parameter, system returns nonzero if command processor exists (or system can run). Otherwise returns 0.
Note that the above programs may not work on online compiler as System command is disabled in most of the online compilers including GeeksforGeeks IDE.
This article is contributed by Subhankar Das. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Источник