getline问题 [英] getline problems

查看:63
本文介绍了getline问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用这样的代码


string s

while(getline(cin,s) )){

process(s);

}

//这是最后一行

流程( s);


进程进行一些替换和重新扫描。问题是我不能用b $ b剥离或添加换行符。所以我不知道最后一行是否包含

''\ n''或者它只是EOF。


我不能读取整个缓冲区,因为它太大而且有些线路不需要更换b $ b。


使用while(fgets(str,MAX, stdin){

s = str

}

可以工作但是再次将str转换为s是一种开销。

您能否就如何克服此问题提出建议?

解决方案

Terry IT写道:




我正在使用这样的代码


string s

while( getline(cin,s)){

process(s);

}

//这是最后一行

process(s);



这是错误的。循环后s不会有新数据。


11月9日上午9:19 *,red floyd< no.spam.h ... @ example.comwrote:


Terry IT写道:


hi,

*我正在使用这样的代码


string s

* while(getline(cin,s)){

* *过程;

*}

//这是最后一行

* process(s);



这是错误的。循环后* s将不会有新数据。



i如果文件不包含换行符,则s包含所有字符

直到流结束。


Terry IT写道:


11月9日9:19 * am,red floyd< no.spam.h ... @ example.comwrote:


> Terry IT写道:


hi,

我正在使用这样的代码


string s

while(getline(cin,s)) {

流程;

}

//这是最后一行

流程;


这是错的。循环后* s将不会有新数据。



我认为如果文件不包含换行符,则s包含所有字符

直到流结束。



该点不是包含的内容。关键是你要两次处理

最后一行。这可能不是你想要的。

最好


Kai-Uwe Bux


hi,
i''m using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

process does some replacement and rescanning. The problem is i can''t
strip or add newlines. So i don''t know whether the lastline contains a
''\n'' or it was just EOF.

I can''t read the whole buffer as it is too huge and some line doesn''t
need to be replaced.

Using while(fgets(str,MAX,stdin){
s=str
}
works but again this conversion of str to s is an overhead.

Can you suggest on how to overcome on this getline issue ?

解决方案

Terry IT wrote:

hi,
i''m using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

This is wrong. s will not have new data after the loop.


On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:

Terry IT wrote:

hi,
*i''m using code like this

string s
*while(getline(cin,s)){
* * process(s);
* }
// this is the last line
* process(s);


This is wrong. *s will not have new data after the loop.

i thought if file contains no newline ,then s contains all the chars
until the end of stream.


Terry IT wrote:

On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:

>Terry IT wrote:

hi,
i''m using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);


This is wrong. *s will not have new data after the loop.


i thought if file contains no newline ,then s contains all the chars
until the end of stream.

The point is not what s contains. The point is that you are processing the
last line twice. That is probably not what you want.
Best

Kai-Uwe Bux


这篇关于getline问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆