Replace std::cin.get with getchar. Fix #334

To avoid C++ iostream headache:
std::cin has a bad() state
std::{cin,cout} are also tied by default, reading from cin will cause a flush of cout, which may deadlock
This commit is contained in:
Fangrui Song 2018-01-24 21:32:05 -08:00
parent 61efdcc721
commit d2c4c34bc0
3 changed files with 10 additions and 8 deletions

View File

@ -32,7 +32,7 @@
#include <rapidjson/error/en.h> #include <rapidjson/error/en.h>
#include <loguru.hpp> #include <loguru.hpp>
#include <climits> #include <stdio.h>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
@ -478,7 +478,7 @@ int main(int argc, char** argv) {
if (HasOption(options, "--wait-for-input")) { if (HasOption(options, "--wait-for-input")) {
std::cerr << std::endl << "[Enter] to exit" << std::endl; std::cerr << std::endl << "[Enter] to exit" << std::endl;
std::cin.get(); getchar();
} }
if (print_help) { if (print_help) {

View File

@ -5,6 +5,7 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include <loguru.hpp> #include <loguru.hpp>
#include <stdio.h>
#include <iostream> #include <iostream>
MessageRegistry* MessageRegistry::instance_ = nullptr; MessageRegistry* MessageRegistry::instance_ = nullptr;
@ -107,8 +108,8 @@ TEST_SUITE("FindIncludeLine") {
} }
optional<char> ReadCharFromStdinBlocking() { optional<char> ReadCharFromStdinBlocking() {
char c; int c = getchar();
if (std::cin.read(&c, 1)) if (c >= 0)
return c; return c;
return nullopt; return nullopt;
} }

View File

@ -5,6 +5,7 @@
#include "serializer.h" #include "serializer.h"
#include "utils.h" #include "utils.h"
#include <stdio.h>
#include <iostream> #include <iostream>
void Write(const std::vector<std::string>& strs) { void Write(const std::vector<std::string>& strs) {
@ -96,8 +97,8 @@ std::string FindExpectedOutputForFilename(
} }
std::cerr << "Couldn't find expected output for " << filename << std::endl; std::cerr << "Couldn't find expected output for " << filename << std::endl;
std::cin.get(); getchar();
std::cin.get(); getchar();
return "{}"; return "{}";
} }
@ -243,8 +244,8 @@ bool RunIndexTests(const std::string& filter_path, bool enable_update) {
<< "[Enter to continue - type u to update test, a to update all]"; << "[Enter to continue - type u to update test, a to update all]";
char c = 'u'; char c = 'u';
if (!update_all) { if (!update_all) {
c = (char)std::cin.get(); c = getchar();
std::cin.get(); getchar();
} }
if (c == 'a') if (c == 'a')