getline - 有点 [英] getline - sort of

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

问题描述

这必须是常见问题解答 - 或几个常见问题解答 - 但我看起来不太好b / b
来确定它。


我需要阅读来自stdin的字符串,然后我将用sscanf处理数字

。我需要限制读取的字符数,并丢弃该行的剩余部分。我还需要检测何时输入更多或更少的字符

。有点像GNU getline我想。


有没有标准的便携方式来实现这个带库函数,

或者我只是自己编写它进程输入一个字符在

a时间?


这必须一直出现。很抱歉这样的针头。司机

我可以办理。用户输入,完全是另一件事...


谢谢,

比尔

-

William D Waddington
wi *********** *****@beezmo.com

即使错误......也是意想不到的路标

漫长的创造之路...... - Ken Burtch

This must be a FAQ - or several FAQs - but I can''t quite seem
to pin it down.

I need to read a string from stdin which I will then process as digits
with sscanf. I need to limit the # of chars read, and discard the
rest of the line. I also need to detect when more or fewer chars
are input. Something like GNU getline I guess.

Is there a standard portable way to do this w/a library function,
or do I just write it myself and process input a single char at
a time?

This must come up all the time. Sorry to be such a pinhead. Drivers
I can handle. User input, that another thing entirely...

Thanks,
Bill
--
William D Waddington
wi****************@beezmo.com
"Even bugs...are unexpected signposts on
the long road of creativity..." - Ken Burtch

推荐答案



" Bill Waddington" < wi **************** @ beezmo.com写了留言

"Bill Waddington" <wi****************@beezmo.comwrote in message

这必须是常见问题 - 或者几个常见问题解答 - 但我看起来不太好b / b
来固定它。


我需要从stdin中读取一个字符串,然后我将处理为数字

与sscanf。我需要限制读取的字符数,并丢弃该行的剩余部分。我还需要检测何时输入更多或更少的字符

。有点像GNU getline我想。


有没有标准的便携方式来实现这个带库函数,

或者我只是自己编写它进程输入一个字符在

a时间?


这必须一直出现。很抱歉这样的针头。司机

我可以办理。用户输入,完全是另一件事...
This must be a FAQ - or several FAQs - but I can''t quite seem
to pin it down.

I need to read a string from stdin which I will then process as digits
with sscanf. I need to limit the # of chars read, and discard the
rest of the line. I also need to detect when more or fewer chars
are input. Something like GNU getline I guess.

Is there a standard portable way to do this w/a library function,
or do I just write it myself and process input a single char at
a time?

This must come up all the time. Sorry to be such a pinhead. Drivers
I can handle. User input, that another thing entirely...



获得无界限输入线没有什么好方法。


如果输入是行分隔数字,最好的办法是用适当的超量缓冲区调用

fgets()。

拒绝任何输入其中没有尾随换行符。

然后调用strtod()将.line转换为数字。


char buff [256];

double x;

char * end;


fgets(buff,256,stdin);

if (!strchr(buff," \ n''))

{

邪恶的人正在传递一条长线的坏线

}

x = strtod(buff,& end);

if(end == buff)

{

邪恶的人通过了一个不是数字的行

}


此时x保存数字,结束最后一个字符的addfress

之后。您可能会丢弃或进一步处理。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


Malcolm McLean写道,于16/01/08 17:39:
Malcolm McLean wrote, On 16/01/08 17:39:

>

" Bill Waddington" < wi **************** @ beezmo.com写了留言
>
"Bill Waddington" <wi****************@beezmo.comwrote in message

>这必须是常见问题 - 或者几个常见问题解答 - 但我似乎无法将它固定下来。

我需要从stdin中读取一个字符串,然后我将使用sscanf处理数字
。我需要限制读取的字符数,并丢弃该行的其余部分。我还需要检测何时输入更多或更少的字符。像GNU getline这样的东西我想。

是否有一种标准的可移植方式来实现这个带有库函数,或者我只是自己编写它并在一次?

这必须一直出现。很抱歉这样的针头。司机
我可以处理。用户输入,完全是另一件事...
>This must be a FAQ - or several FAQs - but I can''t quite seem
to pin it down.

I need to read a string from stdin which I will then process as digits
with sscanf. I need to limit the # of chars read, and discard the
rest of the line. I also need to detect when more or fewer chars
are input. Something like GNU getline I guess.

Is there a standard portable way to do this w/a library function,
or do I just write it myself and process input a single char at
a time?

This must come up all the time. Sorry to be such a pinhead. Drivers
I can handle. User input, that another thing entirely...



获得无界限输入线没有什么好方法。


如果输入是行分隔数字,你最好的选择是使用适当超量的缓冲区来调用fgets()。

There''s no really good way of getting an unbounded input line.

Probably if the input is line-delimited numbers, your best bet is to
call fgets() with a suitably over-lrage buffer.



由于OP要求上限,过大的缓冲区

*不需要*。 OP需要一个正确大小的缓冲区,允许换行和/ 0终止


Since the OP has a requirement for an upper limit an overly large buffer
is *not* required. The OP needs a buffer of the correct size allowing
for the newline and /0 termination.


拒绝任何没有输入的输入尾随换行符。
Reject any input for which there is no trailing newline.



OP在哪里说拒绝? OP应该使用缺少换行符

表示太长的线路和循环丢弃其余部分,并且早期

换行符表示线路太短。 />

Where did the OP say reject? The OP should use lack of a newline to
indicate too long a line and loop to discard the rest, and an early
newline to indicate too short a line.


然后调用strtod()将.line转换为数字。
Then call strtod() to convert the .line to a number.



OP在哪里指定浮点数?我同意strto

函数通常比sscanf更容易使用,但由于OP不提供
提及小数点或符号strtoul更有可能被使用。

Where did the OP specify a floating point number? I agree that the strto
functions are generally easier to use than sscanf but as the OP does not
mention decimal points or signs strtoul is more likely to be of use.


char buff [256];
char buff[256];



Manifest常量不好。

Manifest constants are bad.


double x;

char *结束;


fgets(buff,256,stdin);
double x;
char *end;

fgets(buff, 256, stdin);



这里你重复你的清单常量导致更多的机会导致错误。


#define(或枚举)缓冲区上使用sizeof的缓冲区大小(它将是缓冲区而不是指针传入的缓冲区,这似乎很可能。


此外fgets返回一个值。未检查它意味着如果发生文件结束或错误,你将不会知道

,这两个*都可以*发生在stdin上。 />

Here you repeat your manifest constant leading to a higher chance of
introducing bugs.

Either #define (or enum) the buffer size of use sizeof on the buffer (it
it will be the buffer not a pointer passed in, which seems likely.

Also fgets returns a value. Failure to check it means you will not know
if an end-of-file or error occurs, both of which *can* occur on stdin.


if(!strchr(buff," \ n''))
if(!strchr(buff, "\n''))



strchr接受角色作为一个int值,不是(指向)字符串。

此外,OP可能想要使用返回的值来了解

多长的行。所以应该更像是:

eolptr = strchr(buff,''\ n'');

if(!eolptr)

strchr takes the character as an int value, NOT a (pointer to) string.
Also the OP probably wants to use the value returned to find out how
long the line was. So that should be more like:
eolptr = strchr(buff,''\n'');
if (!eolptr)


{

邪恶的人正在传递一条长线超过
{
evil person is passing you a bad line that is over long



执行循环读取,直到获得EOF或/ n。我会使用getc

而不是fgetc,因为它打算成为一个宏而且

因此使优化器工作变得更容易。

Do a loop reading until you get either EOF or /n. I would use getc
rather than fgetc for this since it is intended to be a macro and
therefore makes the optimisers job easier.


}
}



else

linelen = eolptr-buf;

else
linelen = eolptr-buf;


x = strtod(buff,& end);

if(end == buff)

{

邪恶的人通过一行不是数字
x = strtod(buff, &end);
if(end == buff)
{
evil person passed a line that was not a number



或者是以空格或

数字以外的东西开头的。我们不知道这是无效的,这可能是OP

想要使用sscanf的原因。

Or something that started with something other than white space or a
number. We do not know that this is invalid and it might be why the OP
wants to use sscanf.


}


此时x保持数字,结束最后一个字符后面的addfress

。您可以放弃或进一步处理。
}

at this point x holds the number, end the addfress of the last character
after it. You might discard, or process further.



或者它可能包含INF或NAN。还应该检查errno(在调用之前将
归零)以便可以轻松检测到溢出。

-

Flash Gordon

Or it could contain an INF or NAN. Also errno should be checked (having
been zeroed prior to the call) so that overflow can easily be detected.
--
Flash Gordon


" Flash Gordon" < sp ** @ flash-gordon.me.ukwrote in message
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message

>
>

>此时x保存数字,结束后面的最后一个字符的addfress。您可以放弃或进一步处理。
>at this point x holds the number, end the addfress of the last character
after it. You might discard, or process further.



或者它可能包含INF或NAN。还应检查errno(在调用之前将
归零)以便可以轻松检测到溢出。


Or it could contain an INF or NAN. Also errno should be checked (having
been zeroed prior to the call) so that overflow can easily be detected.



那是'为什么strtod是更好的选择,除非你真的需要大量和

精确整数。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac .uk / ~bgy1mm

That''s why strtod is the better choice, unless you really need massive and
accurate integers.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


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

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