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

View File

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

View File

@ -5,6 +5,7 @@
#include "serializer.h"
#include "utils.h"
#include <stdio.h>
#include <iostream>
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::cin.get();
std::cin.get();
getchar();
getchar();
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]";
char c = 'u';
if (!update_all) {
c = (char)std::cin.get();
std::cin.get();
c = getchar();
getchar();
}
if (c == 'a')