从输入获得的数字最好的方法? [英] Best way to get numbers from input?
问题描述
我想循环逐个字符从有文字和数字的输入文件。我想我可能只是环......
I would like to loop character by character from an input file that has text and numbers. I thought I could just loop...
char count;
while( c != ' ' && c != '\n' && c != '\t' ) {
count += c;
c = fgetc(fp);
}
说从文本文件中得到11,然后使用的atoi转换为int但后来我意识到,我只是将ASCII码。我是相当新的C和从我了解的字符串只是字符数组 - 这是否意味着我必须把1和1变成一个字符数组?但后来我不得不担心数组大小和转换,要多少?
to say get "11" from the text file and then use atoi to convert to an int but I then realized I'm just adding ASCII numbers. I'm fairly new to C and from what I understand strings are just char arrays - does this mean I have to put "1" and "1" into a char array? But then I would have to worry about array size and converting that to a number?
推荐答案
虽然它看起来像自然的解决方案,我通常反对使用意见的fscanf()
上潜在的恶意输入
While it looks like the natural solution, I usually advice against using fscanf()
on potentially malformed input.
有几个问题的功能,其中包括:
There are several issues with the function, among them:
- 无法从错误(短
FTELL()
/fseek的()
),因为正常恢复你不知道在哪里的究竟的它停止解析输入。 - 与它导致使用未初始化值的难易程度(如果格式未能匹配输入,而你没有正确检查返回code)。
- 某些角落情况下,
的fscanf()
扼流圈输入其中,的atoi()
/与strtol()
不(<一个href=\"http://stackoverflow.com/questions/1425730/difference-between-scanf-and-strtol-strtod-in-parsing-numbers\">\"0xz\"...).
- The inability to recover gracefully from an error (short of
ftell()
/fseek()
), because you don't know where exactly it stopped parsing input. - The ease with which it leads to using uninitialized values (if the format failed to match the input, and you didn't check the return code properly).
- Some corner cases where
fscanf()
chokes on input whereatoi()
/strtol()
don't ("0xz"...).
这一切,遇事推诿的fscanf()
阅读的格式正确的唯一的输入,也就是你自己的程序曾在一个已知的格式之前写的东西。
All this relegates fscanf()
to read well-formatted input only, i.e. things your own program had written before in a known format.
对于任何类型的输入是可能的不的是预期的格式,我建议每次读一本线(与fgets()
),并分析其内存,例如使用的 与strtol()
:
For any kind of input that might not be in the expected format, I recommend reading a line at a time (fgets()
), and parsing it in-memory, for example with strtol()
:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define LINE_BUFFER_SIZE 256;
// ...
char line[ LINE_BUFFER_SIZE ];
fgets( line, LINE_BUFFER_SIZE, fp );
char * scanptr;
errno = 0;
long number = strtol( line, &scanptr, 0 );
if ( scanptr == line )
{
// not a number...
}
-
scanptr
现在指向到数字分析结束。 -
数量
包含数字解析,0,如果行没有用号,LONG_MAX
RESP开始。LONG_MIN
如发现数量超出长
范围(在这种情况下,的errno == ERANGE
以及)。 scanptr
now points to where number parsing ended.number
contains the number parsed, 0 if line did not start with a number, andLONG_MAX
resp.LONG_MIN
if the number found is out oflong
range (in which caseerrno == ERANGE
as well).
我不知道您的具体要求,所以我不能给,因为如果你没有找到一个号码,或做什么用文字做什么一个很好的例子。但是,因为你得到了线(或更长行的第一个 LINE_BUFFER_SIZE
字符...)在内存中,你有的字符串函数的在您的处置。 strpbrk(行,0123456789)
可以用来搜索下一个数字或 strpbrk(行,+ -0123456789)
如果您可能有 + -
在输入...
I don't know your exact requirements, so I cannot give a good example for what to do if you don't find a number, or what to do with the text. But since you got the line (or, the first LINE_BUFFER_SIZE
characters of a longer line...) in memory, you have the whole range of the string functions at your disposal. strpbrk( line, "0123456789" )
could be used to scan for the next digit, or strpbrk( line, "+-0123456789" )
if you might have +-
in your input...
这篇关于从输入获得的数字最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!