[英]running shell scripts in c++
Ive been writing the folowing code: 我一直在写下面的代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << "The script will be executed";
system("./simple.sh");
}
But when I run it the shell script is executed first. 但是当我运行它时,首先执行shell脚本。
What can I do to execute "cout << "The script will be executed"" first? 我该怎么做才能执行“cout <<”脚本将首先执行?
Flushing the output stream buffer should be enough. 刷新输出流缓冲区应该足够了。 You can do this with 你可以这样做
cout << "The script will be executed";
cout.flush();
Alternatively, if you intended to also print a newline character then you can use std::endl
which implicitly flushes the buffer: 或者,如果您还打算打印换行符,则可以使用std::endl
隐式刷新缓冲区:
cout << "The script will be executed" << endl;
You're not flushing the output stream. 你没有刷新输出流。
Try: 尝试:
cout << "The script will be executed" << endl; // or cout.flush()
system("./simple.sh");
The script is executed second, the delay between the call to cout
and printing to the console is probably throwing you off. 该脚本执行第二次,调用cout
和打印到控制台之间的延迟可能会让你失望。
you can use cerr << "The script will be executed";
你可以使用cerr << "The script will be executed";
when you're trying to print something using cout
it keeps that in buffer. 当你尝试使用cout
打印某些内容时,它会将其保留在缓冲区中。 cerr
is printing immediately but @Jon answered better. cerr
立即打印,但@Jon回答得更好。
尝试在调用system()syscall之前刷新stdout流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.