使用文件提取运算符读取int或string [英] Read whether int or string using the file extraction operator

查看:239
本文介绍了使用文件提取运算符读取int或string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个文件读取,我使用提取运算符来获取第一个值。问题是我需要知道的值是一个int还是一个字符串,所以我可以把它放在适当的变量。



所以我的问题是我可以尝试它在一个int,如果它失败,它会抛出一个字符串?或者事先检查它的int或字符串?



如果需要,我可以提供代码/ psudocode。



文件示例:

  3行3列全部@ go 
5列6行go
5行triangle go
alphabetical 3行3列go
all! 4 rows 4 columns outer go
字母三角形外部5行go


解决方案

有很多方法可以做到这一点,一个是简单地将其读为字符串,并尝试在您自己的代码中将其解析为一个整数。如果解析成功,那么你有一个整数,否则你有一个字符串。



有几种方法来解析字符串,包括但不限于: p>






使用 std :: stoi

  std :: string input; 
if(std :: getline(std :: cin,input))
{
try
{
std :: size_t pos;
int n = std :: stoi(input,& pos);

//输入以数字开头,但可能包含其他数据
if(pos< input.length())
{
//不所有输入是一个数字,包含一些非数字
//字符作为位置`pos`
}
else
{
//输入是一个数字
}
}
catch(std :: invalid_argument&)
{
//输入不是数字,将其视为字符串
}
catch(std :: out_of_range&)
{
//输入是一个数字,但它是大和溢出
}
}

如果你不想使用异常,那么旧的C函数 std :: strtol

  std :: string input; 
if(std :: getline(std :: cin,input))
{
char * end = nullptr;
long n = std :: strtol(input.c_str(& end,10);

if(n == LONG_MAX&& errno == ERANGE)
{
//输入包含一个数字,但它是大和owerflows
}
else if(end == input.c_str())
{
//输入不是一个数字,处理为字符串
}
else if input.c_str()+ input.length())
{
//输入是一个数字
}
else
{
//输入以数字开头,但也包含一些
//非数字字符
}
}


I'm reading from a file and I am using the extraction operator to grab the first value. the problem is i need to know if the value is a int or a string so i can put it in the appropriate variable.

so my question is can I try to put it in an int and if it fails it'll throw it in a string? or check beforehand if its a int or a string?

I can provide code/psudocode if need be.

file example:

   3 rows  3 columns all @ go
        5 columns 6  rows go
        5     rows        triangular        go
alphabetical   3 rows    3       columns go
 all !  4 rows  4 columns outer     go
 alphabetical      triangular       outer      5 rows   go

解决方案

There are many ways to do it, one is to simply read it as a string, and the try to parse it as an integer in your own code. If the parsing succeeds then you have an integer otherwise you have a string.

There are a few ways to parse a string, including (but not limited to):

  • Using std::istringstream and the >> operator, and check the stream flags
  • std::stoi
  • Programatically check if all characters are digits, convert using normal decimal arithmetic.

Simple example using std::stoi:

std::string input;
if (std::getline(std::cin, input))
{
    try
    {
        std::size_t pos;
        int n = std::stoi(input, &pos);

        // Input begins with a number, but may contain other data as well
        if (pos < input.length())
        {
            // Not all input was a number, contains some non-digit
            // characters as position `pos`
        }
        else
        {
            // Input was a number
        }
    }
    catch (std::invalid_argument&)
    {
        // Input is not a number, treat it as a string
    }
    catch (std::out_of_range&)
    {
        // Input is a number, but it's to big and overflows
    }
}

If you don't want to use exceptions, then the old C-function std::strtol may be used instead:

std::string input;
if (std::getline(std::cin, input))
{
    char* end = nullptr;
    long n = std::strtol(input.c_str(), &end, 10);

    if (n == LONG_MAX && errno == ERANGE)
    {
        // Input contains a number, but it's to big and owerflows
    }
    else if (end == input.c_str())
    {
        // Input not a number, treat as string
    }
    else if (end == input.c_str() + input.length())
    {
        // The input is a number
    }
    else
    {
        // Input begins with a number, but also contains some
        // non-number characters
    }
}

这篇关于使用文件提取运算符读取int或string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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