- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道为什么下面的代码没有按预期工作:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
string s;
//scanf("%d",&n);
cin >> n;
ios_base::sync_with_stdio(false);
cin >> s;
cout << n << " " << s;
return 0;
}
输入:
10
abcd
输出:10
字符串没有被打印出来!如果我使用 scanf
结果是一样的输入整数。但是,如果 ios_base::sync_with_stdio(false);
行,代码确实会按预期工作。放在第一个 cin
之前(或在 scanf
之前)代替。
如果您能帮助澄清此行为,我将不胜感激。
编辑:输入包含在文件 inp.txt
中我正在使用 <
(重定向运算符)从文件中读取和 >
将结果输出到out.txt
, 喜欢: a.out < inp.txt > out.txt
如果输入是直接从控制台给出的,代码会给出预期的输出。对于任何误解或混淆,我们深表歉意。
最佳答案
调用 sync_with_stdio
在 I/O 执行后导致实现定义的行为(这比 cppreference 的“有任何影响”的措辞更强):
Effects: If any input or output operation has occurred using the standard streams prior to the call, the effect is implementation-defined. Otherwise, called with a false argument, it allows the standard streams to operate independently of the standard C streams.
在 libc++ 中,什么也不会发生,因为它所做的只是切换一个标志。在 libstdc++ 中,它实际上执行 logic切换流。任何进一步尝试使用流都会导致失败:
cin >> s;
cin.clear();
cin >> s;
std::cout << n << " " << s;
无论哪种方式,the libstdc++ manual声明您需要在 执行 I/O 之前调用函数。所以你现在实际拥有的是未定义的行为。
关于c++ - ios_base::sync_with_stdio(false) 在来自标准输入的两个输入之间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468606/
#include #include #include using namespace std; class Logger { private: ofstrea
FatalMessageAssembler是一个通过 FatalMessageAssembler& operator FatalMessageAssembler& operator::basic_i
我正在尝试将一个复杂对象保存到一个文件中,我正在像这样在复杂对象中重载 > 运算符 class Data { public: string name; double rating; f
当我尝试执行以下操作时,标题中出现错误。 class Test { private: std::ifstream File; public: Test(); }; 测试::测试() {
我有一个看起来像这样的结构: sturct person { string surname; person(string n) : surname(n) {}; } 我需要重载 ope
答: std::ofstream("test.txt", std::ios_base::app); 乙: std::ofstream("test.txt", std::ios_base::app|st
头文件中可以看出 , 类 ios_base源自 template class _Iosb ,其中以下 static const variables定义: static const _Fmtflag
这是来自MSDN的文档:ate,在首次创建控制对象时寻找流的末尾。 trunc,在创建控制对象时删除现有文件的内容。 我只是无法理解它们之间的区别,以下两个代码片段的行为相同(它们在插入之前清除内容)
我有一个返回 unique_ptr 的 API给 API 用户。我想知道用户何时完成此流,以便我可以对他们刚刚写入的文件采取进一步的操作。必须关闭该文件,因为即将重新挂载分区。 这可能是这个问题的错误
27.6.3.4.2 Buffer management and positioning pos_type seekoff(off_type off, ios_base::seekdir way,
我一直在寻找将字符串转换为 int 的最佳方法,但遇到了一个我不理解的函数: template bool from_string(T& t, const std::
std::ios_base::ate(例如 std::ios_base::app 除外)和 std::的意义何在ios_base::trunc(例如std::ios_base::out除外)? 我应该
在 std::ios_base::out 中使用 std::ios_base::trunc 标志的目的是什么?我在很多例子中都看到了这一点。 我认为标准保证 std::ios_base::out 也会
对于我的一项作业,我被告知使用 cin.clear(ios_base::failbit) 来设置 failbit。我想知道,cin.clear(ios_base::failbit) 和 cin.set
我已经实现了查找最大值和最小值的函数。但我得到以下错误代码: [错误] 从“std::ios_base& (*)(std::ios_base&)”到“int”的无效转换 [-fpermissive]
ios_base 类中的静态常量在创建时进行初始化,这对于常量来说是有意义的。非常量静态成员变量是否可以用相同的方式初始化,或者这个概念是否只允许常量静态成员使用? 对于带有 gnu 编译器的非常量静
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
这个函数有两个不同的引用: 一个用 const 声明的 here ; 还有一个没有 const here ; 哪个是正确的? 最佳答案 标准说 (27.5.3.3) 定义应该是: locale get
我正在使用 libcurl 下载序列化代码并将其打开,但是,我收到一个错误,看起来 fstream 丢失了,但它包含在内。我环顾四周,但很少发现错误。下面是错误和代码。错过了什么? 编译错误输出 g+
我想知道为什么下面的代码没有按预期工作: #include #include using namespace std; int main(){ int n; string s;
我是一名优秀的程序员,十分优秀!