流输入 [英] stream input

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

问题描述

嗨伙计们,

我很清楚使用流和运营商的优势<< />>

而不是旧式的printf和scanf。但是,只有一个

的障碍阻止我切换到更好的形式。

scanf具有以下非常方便的语法:


fscanf(pFile," prfl num:%d \ n",& nPrflNum);

关于它的便利部分是它几乎是<的精确副本br />
创建输入的fprintf操作。特别是它允许通过写出它的内容来跳过特定文本上的



用方便的输入替换它似乎很难流

版,例如:

输入>>" prfl num:">> nPrflNum;


这段代码结果证明是非法的,因为输入流类并不知道如何输入常量字符串。我知道的解决方法非常麻烦,包括逐个读取字符,并验证它们是否适合prfl num:模式,或忽略一切都在流中找到一个数字

字符。


有没有更直接/无痛的方式来做我想做的事情?


干杯,

Aleks D.

解决方案



Aleks Dubinskiy < ad@imm.dtu.dk>在消息中写道

news:cg ********** @ news.net.uni-c.dk ...

嗨大家好,而不是旧式的printf和scanf。但是,只有一个障碍阻止我切换到更好的形式。
scanf具有以下非常方便的语法:

fscanf(pFile," prfl num: %d \ n",& nPrflNum);
关于它的方便部分是它几乎是创建输入的
fprintf操作的精确副本。特别是它允许通过写出它是什么来跳过特定文本。

用一个方便的输入
流版本替换它似乎非常困难,例如as:
input>>" prfl num:">> nPrflNum;

此代码证明是非法的,因为输入流类不是非常繁琐,包括逐个读取字符,并验证它们是否适合prfl num:模式,或忽略所有内容直到数字
在溪流中找到了角色。

是否有更简单/无痛的方式来做我想做的事情?

干杯,
Aleks D.


std :: ifstream输入;

std :: string s;

int num(0);

/ * etc * /

std :: getline(input,s);

if(s ==" prfl num:")

输入>> num;


-Mike



Aleks Dubinskiy写道:

大家好,
我很清楚使用流和运营商的优势<< />>
而不是旧式的printf和scanf。但是,只有一个障碍阻止我切换到更好的形式。
scanf具有以下非常方便的语法:

fscanf(pFile," prfl num: %d \ n",& nPrflNum);
关于它的方便部分是它几乎是创建输入的
fprintf操作的精确副本。特别是它只需要写出它就可以跳过特定的文本。

用一个方便的输入流
版本替换它似乎很困难。 as:
input>>" prfl num:">> nPrflNum;

此代码证明是非法的,因为输入流类不是字符在流中找到。

是否有更直接/无痛的方式来做我想要的事情?



可能符合
的内容

模板< class S> S&安培; skipover(S& s,const char * str){

返回s.seekg(fpos(strlen(str),S :: cur);

}

...

skipover(输入," prfl num:")>> nPrflNum;


我相信大家好会纠正我错误的地方。


V


Mike Wahler写道:

" Aleks Dubinskiy"< ad@imm.dtu.dk>写在消息中
新闻:cg ********** @ news.net.uni-c.dk ...
< blockquote class =post_quotes>大家好,
我很清楚使用流和运算符<< />>
而不是旧式printf和scanf的优越性。 ,只有一个障碍阻止我切换到更好的形式。
scanf具有以下非常方便的语法:

fscanf(pFile," prfl num:% d \ n",& nPrflNum);
关于它的方便部分是它几乎是创建了它的
fprintf操作的精确副本输入。特别是它允许通过写出它是什么来跳过特定文本。

用方便的输入替换它似乎很困难



stream

版本,例如:
input>>" prfl num:">> nPrflNum;
<这段代码结果证明是非法的,因为输入流类并不知道如何输入常量字符串。我知道的解决方法是



繁琐,涉及逐个阅读字符,并验证它们是否适合模式prfl num:,或忽略所有内容,直到在流中找到数字
字符。

是否有更直接/无痛的方式来做我想要的?

干杯,
Aleks D。



std :: ifstream输入;
std :: string s;
int num (0);
/ * etc * /
std :: getline(input,s);
if(s ==" prfl num:")
输入> ;> num;




我不知道如何读取行中的整数如何

prfl num:42


(由对称声明打印


of<<<" prfl num:"<<< some_int_with_value_42< ;< std :: endl;

}


不会'getline''跳过数字并读到行尾

因此失去了42?


Victor


Hi folks,
I am well aware of the superiority of using streams and operators << / >>
instead of the old-style printf and scanf. However, there is only one
obstacle that prevents me from switching to the better form.
scanf has the following really convenient syntax:

fscanf(pFile, "prfl num: %d\n", &nPrflNum);
The convenient part about it is that it''s almost an exact copy of the
fprintf operation that created the input. In particular it allows to skip
over a particular text by just writing what it is.

It seems to be very difficult to replace this with a convenient input stream
version, such as:
input>>"prfl num: ">>nPrflNum;

This code turns out to be illegal, because the input stream class doesn''t
know how to input constant strings. The workaround I know for this is quite
cumbersome, involving reading characters one by one, and verifying if they
fit the pattern "prfl num: ", or ignoring everything until a numeric
character is found in the stream.

Is there a more straightforward / painless way to do what I want?

Cheers,
Aleks D.

解决方案


"Aleks Dubinskiy" <ad@imm.dtu.dk> wrote in message
news:cg**********@news.net.uni-c.dk...

Hi folks,
I am well aware of the superiority of using streams and operators << / >>
instead of the old-style printf and scanf. However, there is only one
obstacle that prevents me from switching to the better form.
scanf has the following really convenient syntax:

fscanf(pFile, "prfl num: %d\n", &nPrflNum);
The convenient part about it is that it''s almost an exact copy of the
fprintf operation that created the input. In particular it allows to skip
over a particular text by just writing what it is.

It seems to be very difficult to replace this with a convenient input stream version, such as:
input>>"prfl num: ">>nPrflNum;

This code turns out to be illegal, because the input stream class doesn''t
know how to input constant strings. The workaround I know for this is quite cumbersome, involving reading characters one by one, and verifying if they
fit the pattern "prfl num: ", or ignoring everything until a numeric
character is found in the stream.

Is there a more straightforward / painless way to do what I want?

Cheers,
Aleks D.
std::ifstream input;
std::string s;
int num(0);
/* etc */
std::getline(input, s);
if(s == "prfl num: ")
input >> num;

-Mike



Aleks Dubinskiy wrote:

Hi folks,
I am well aware of the superiority of using streams and operators << / >>
instead of the old-style printf and scanf. However, there is only one
obstacle that prevents me from switching to the better form.
scanf has the following really convenient syntax:

fscanf(pFile, "prfl num: %d\n", &nPrflNum);
The convenient part about it is that it''s almost an exact copy of the
fprintf operation that created the input. In particular it allows to skip
over a particular text by just writing what it is.

It seems to be very difficult to replace this with a convenient input stream
version, such as:
input>>"prfl num: ">>nPrflNum;

This code turns out to be illegal, because the input stream class doesn''t
know how to input constant strings. The workaround I know for this is quite
cumbersome, involving reading characters one by one, and verifying if they
fit the pattern "prfl num: ", or ignoring everything until a numeric
character is found in the stream.

Is there a more straightforward / painless way to do what I want?


Probably something in line with

template<class S> S& skipover(S& s, const char* str) {
return s.seekg(fpos(strlen(str), S::cur);
}
...
skipover(input, "prfl num: ") >> nPrflNum;

I am sure folks will correct me where I erred.

V


Mike Wahler wrote:

"Aleks Dubinskiy" <ad@imm.dtu.dk> wrote in message
news:cg**********@news.net.uni-c.dk...

Hi folks,
I am well aware of the superiority of using streams and operators << / >>
instead of the old-style printf and scanf. However, there is only one
obstacle that prevents me from switching to the better form.
scanf has the following really convenient syntax:

fscanf(pFile, "prfl num: %d\n", &nPrflNum);
The convenient part about it is that it''s almost an exact copy of the
fprintf operation that created the input. In particular it allows to skip
over a particular text by just writing what it is.

It seems to be very difficult to replace this with a convenient input



stream

version, such as:
input>>"prfl num: ">>nPrflNum;

This code turns out to be illegal, because the input stream class doesn''t
know how to input constant strings. The workaround I know for this is



quite

cumbersome, involving reading characters one by one, and verifying if they
fit the pattern "prfl num: ", or ignoring everything until a numeric
character is found in the stream.

Is there a more straightforward / painless way to do what I want?

Cheers,
Aleks D.


std::ifstream input;
std::string s;
int num(0);
/* etc */
std::getline(input, s);
if(s == "prfl num: ")
input >> num;



I am not sure how it would help to read the integer in a line like

prfl num: 42

(which was printed by the symmetrical statement

of << "prfl num: " << some_int_with_value_42 << std::endl;
}

Wouldn''t ''getline'' skip over the number and read until the end of line
thus losing the "42"?

Victor


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

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