从输入读取字符串直到EOF [英] read strings from input till EOF
问题描述
我已经阅读了SO的许多帖子,但仍然无法解决这个问题。
我必须阅读:
文字
pattern1
pattern2
,有许多文本和模式
。
代码:
string t,p1,p2;
while(getline(cin,t))
{
cin>> p1;
cin>> p2;
cout<<text is =<< t<<\\\
p1 is =< p2 is =< ; p2 }
输入文件:
锤子
ham
mer
gogoa
go < br>
z
gogoa
g
o
输出:::
p2 is = mer
text is =
p1 is = gogoa
p2 is = go
text is =
p1 is = z
p2 is = gogoa
text is =
p1 is = g
p2 is = o
如果您在 cin>>
$ b
#include< iostream>
#include< limits>
using namespace std;
int main(){
string t,p1,p2;
while(getline(cin,t))
{
cin>> p1;
cin>> p2;
cout<<text is =<< t<<\\\
p1 is =< p2 is =< ; p2 <
cin.ignore(std :: numeric_limits< std :: streamsize> :: max(),'\\\
'); // Dump everything until newline
}
return 0;
}
更多此处:http://stackoverflow.com/a/10553849/1938163
I have gone through many posts on SO , but still i am not able to resolve the issue .
I have to read :
text
pattern1
pattern2
from standard input , there are many text and patterns
.
Code :
string t,p1,p2;
while(getline(cin, t))
{
cin>>p1;
cin>>p2;
cout<<"text is = "<<t<<"\np1 is = "<<p1<<"\np2 is = "<<p2<<endl;
}
Input file :
hammer
ham
mer
gogoa
go
z
gogoa
g
o
Output :
text is = hammer
p1 is = ham
p2 is = mer
text is =
p1 is = gogoa
p2 is = go
text is =
p1 is = z
p2 is = gogoa
text is =
p1 is = g
p2 is = o
If you're using getline after cin >> something
, you need to flush the newline out of the buffer in between.
#include <iostream>
#include <limits>
using namespace std;
int main() {
string t,p1,p2;
while(getline(cin, t))
{
cin>>p1;
cin>>p2;
cout<<"text is = "<<t<<"\np1 is = "<<p1<<"\np2 is = "<<p2<<endl;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); // Dump everything until newline
}
return 0;
}
More here: http://stackoverflow.com/a/10553849/1938163
这篇关于从输入读取字符串直到EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!