使用:同时(CIN>>x)和EOF
use of: while (cin >> x) and eof
我不太确定我在这里的根本问题是如何工作的。我已经阅读了之前关于while(cin>>x(的一些帖子,但似乎没有什么能真正回答我的问题。我正在使用这个循环来读取一些文本数据:
while (cin >> x){
searchText.push_back(x);
}
但稍后在代码中,我尝试使用一个单词阅读:
cout << "Please enter your target word: ";
string targetWord;
cin >> targetWord;
但是上面的 while loop/eof 似乎破坏了第二个代码片段(如果我将第二个代码片段移到上面,一切都可以正常工作,但显然这不是我试图做的(
编辑为清楚起见,以下是完整的代码:
int main()
{
// ask for the target word
// cout << "Please enter your target word: ";
// string targetWord;
// cin >> targetWord;
// ask for and read the search text
cout << "Enter the complete search text, "
"followed by end-of-file: ";
vector<string> searchText;
string x;
while (cin >> x){
searchText.push_back(x);
}
// check that some text was entered
typedef vector<string>::size_type vec_sz;
vec_sz size = searchText.size();
if (size == 0){
cout << endl << "You must enter some text. "
"Please try again." << endl;
return 1;
}
// ask for the target word
cin.clear();
cout << "";
cout << "Please enter your target word: ";
string targetWord;
cin >> targetWord;
int count = 0;
for (int i=0; i<size; ++i){
if (targetWord == searchText[i]){
count++;
}
}
cout << "The target word [" << targetWord << "] was "
"in the search text " << count << " times." << endl;
return 0;
}
我只是想从用户那里接收一些文本......然后是一个搜索词,看看这个词在输入的文本中出现了多少次(非常简单!
我知道我可以以不同的方式做,但这里的问题更多的是关于在之前有 EOF 之后如何再次使用 cout/cin 流
当cin
(或任何其他std::stream
(点击文件末尾时,它会设置一个状态以指示已发生这种情况。
要重置此状态,您需要调用 cin.clear();
,这将清除任何"错误"状态,并确保流"正常"以再次使用。如果您正在读取文件并希望从头开始,这同样适用。
编辑:我只是拿了上面发布的代码,在我的机器上运行它,在顶部添加
#include <iostream>
#include <vector>
using namespace std;
以下是编译和运行:
$ g++ -Wall words.cpp
words.cpp: In function ‘int main()’:
words.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
$ ./a.out
Enter the complete search text, followed by end-of-file: aa bb cc [CTRL-D]
Please enter your target word: aa
The target word [aa] was in the search text 1 times.
这就是我期望看到的...
编辑2:为了完整性:使用cin.clear()
的"成功率"将取决于实现。更可靠的解决方案是在程序的第一阶段使用不同的方式标记单词流的结束。人们可以使用单个"."
或"!"或其他一些不应该出现在"单词"中的东西 - 或者更长的东西,例如"&&@&&"
,但这会使输入和记住输入15页变得困难。
当执行离开循环时
while (cin >> x){
searchText.push_back(x);
}
它这样做是因为cin
的"测试"返回了 false,换句话说,failbit
和/或badbit
已在流中设置。在这种情况下,任何进一步尝试从流中读取都将失败,即 targetWord
将留空。
若要使流再次可用,必须通过调用 cin.clear();
来重置错误标志
如果x
我们知道第一个循环,但基本上:你读取所有可用的输入,然后尝试阅读更多内容,您会惊讶地发现失败。 相反的情况会让我感到惊讶。
真正的问题是:你想做什么? 基本上x
的类型是什么,以及——我假设你是在假设cin
是一个交互式设备,因为prompt - 如何确定第一个输入具有完成? 如果第一个循环由于"文件结束"而结束(用户在Unix下输入control-D,或在Windows下输入control-Z(,然后您不可能可靠地期望阅读更多内容。 重 置cin.clear()
的错误状态可能有效;它会导致尝试从streambuf
中阅读更多内容istream
. 但它可能不会;istream
下方有任意数量的图层无论出于何种原因,它可能已经记住了文件的结尾。所以你必须找到一些不同的方法来识别结束。
只是从变量的名称中猜测:如果您正在尝试要阅读单词列表,我会使用 std::getline
,带有空行作为列表末尾。 所以第一个循环将变成:
while ( std::getline( std::cin, x ) && x != "" ) {
searchText.push_back( x );
}
if ( ! std::cin ) {
// Something really bad has happened...
}
或者,您可能希望将白色上的线分开空格,以允许每行多个单词(并忽略任何行中的额外空格:
std::string line;
while ( std::getline( std::cin, x ) && x != "" ) {
std::istringstream l( line );
while ( l >> x ) {
searchText.push_back( x );
}
}
if ( ! std::cin ) // ...
即使x
有其他类型,您也可能需要考虑类似的东西。
我使用了cin.clear((,在我的Mac设置上,它似乎没有清除EOF,但在ubuntu设置中,它确实清除了流
苹果:
(Canopy 32bit) joes-imac:chap3 joe$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
乌班图:
joe@joe-HPlaptop:~$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
我很高兴接受它只是编译器的差异并完成/关闭问题(真的是一个愚蠢的小家伙!
感谢您的所有帮助(实际上是我的第一个堆栈溢出问题!
遇到尾行字符或换行符时,CIN将停止。因此,每次您输入内容并按回车键时,您都会提供一个""。while(cin>>x({} 应该用于连续读取没有 endline/'' 的输入。
- 如何确定我已使用非编码文件到达 EOF?
- EASTL矢量<向量<int>>连续的
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 而(!inputfile.eof())只读取第一行?
- EOF有更安全的替代方案吗?它在我的情况下不起作用
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 如何在 EOF 之前从文件中读取并将其放入字符串中?
- 如何在从文件中读取整数时使用 file.eof()?
- "eof"可以设置为流吗?
- 吃完 EOF 后重复使用 std::cin
- 文件处理,eof() 定义
- std::cin 在读取 EOF 并清除后不再读取
- 为什么 EOF 在循环扫描期间没有终止?
- cin.clear()是否保留EOF
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 通过终端在文件中输入时检测EOF(文件结束)时出现问题
- 为什么如果我从键盘输入EOF克里昂不要在运行窗口打印程序的输出?
- 为什么"cin"在EOF之后不再wοrk?
- 在 <char>sizeof(int) == 1 的平台中如何编码 char_traits::eof() ?
- 读取 c++ istream 直到 EOF,同时丢弃内容