C
caoliangbj
Hi,
When I capture the SIGINT event, I encounter a probelm.
Please look at the following code, if you don't press any key, the
'getline()' will block there all the time.
When I press 'ctrl+c', the function 'getline()' still block there.
My question is how can I exit from "getline()" while I just press 'ctrl
+c'.
#include <string>
#include <iostream>
using namespace std;
static bool g_bRun = true;
static void OnSIGINT(int)
{
g_bRun = false;
}
int main()
{
Signal(SIGINT, OnSIGINT);
string input;
while (g_bRun) {
if (getline(cin, input)) {
cout << input << endl;
}
}
cout << "exit." << endl;
return 0;
}
Thanks.
When I capture the SIGINT event, I encounter a probelm.
Please look at the following code, if you don't press any key, the
'getline()' will block there all the time.
When I press 'ctrl+c', the function 'getline()' still block there.
My question is how can I exit from "getline()" while I just press 'ctrl
+c'.
#include <string>
#include <iostream>
using namespace std;
static bool g_bRun = true;
static void OnSIGINT(int)
{
g_bRun = false;
}
int main()
{
Signal(SIGINT, OnSIGINT);
string input;
while (g_bRun) {
if (getline(cin, input)) {
cout << input << endl;
}
}
cout << "exit." << endl;
return 0;
}
Thanks.