从具有混合整数,字母和空格的文件中读取整数C ++ [英] Reading integers from a file with mixed integers, letters, and spaces C++

查看:62
本文介绍了从具有混合整数,字母和空格的文件中读取整数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我自己在一周前完成的当前编程作业中添加的一种自我强加的额外信用问题.分配涉及从文件中读取整数,每行有多个整数,每个整数之间用空格分隔.使用while(inFile >> val)即可轻松实现.

This is a sort of self-imposed extra credit problem I'm adding to my current programming assignment which I finished a week early. The assignment involved reading in integers from a file with multiple integers per line, each separated by a space. This was achieved easily using while(inFile >> val) .

我要面对的挑战是尝试从包含数字和字母的混合文件中读取整数,将所有连续的数字提取为由这些数字组成的单独整数.例如,如果我正在从文本文件中读取以下行:

The challenge I put myself up to was to try and read integers from a file of mixed numbers and letters, pulling out all contiguous digits as separate integers composed of those digits. For examples if I was reading in the following line from a text file:

12f 356 48 r56 fs6879 57g 132e efw ddf312 323f

12f 356 48 r56 fs6879 57g 132e efw ddf312 323f

将要读取(存储)的值

12 f 356 48 r 56 fs 6879 57 g 132 e efw ddf 312 323 f

12f 356 48 r56 fs6879 57g 132e efw ddf312 323f

12、356、48、56、6879、57、132、312和323

12, 356, 48, 56, 6879, 57, 132, 312, and 323

我整个下午都在浏览cplusplus.com并阅读封面,以了解get,getline,cin等的详细信息,但我无法为此找到完美的解决方案.我可以推断出的每种方法都涉及从整个文件中详尽地读取每个字符并将其存储到某种容器中,然后一次遍历一个元素并取出每个数字.

I've spent all afternoon digging through cplusplus.com and reading cover to cover the specifics of get, getline, cin etc. and I am unable to find an elegant solution for this. Every method I can deduce involves exhaustive reading in and storing of each character from the entire file into a container of some sort and then going through one element at a time and pulling out each digit.

我的问题是从文件读取它们的过程中是否有办法做到这一点?即get,getline,cin和company的功能是否支持这种复杂的操作?

My question is if there is a way to do this during the process of reading them in from a file; ie does the functionality of get, getline, cin and company support that complex of an operation?

推荐答案

一次读取一个字符并进行检查.有一个变量可以维护当前正在读取的数字,并带有一个标志,告诉您是否正在处理数字.

Read one character at a time and inspect it. Have a variable that maintains the number currently being read, and a flag telling you if you are in the middle of processing a number.

如果当前字符是数字,则将当前数字乘以10,然后将该数字添加到数字中(并设置处理数字"标志).

If the current character is a digit then multiple the current number by 10 and add the digit to the number (and set the "processing a number" flag).

如果当前字符不是数字,并且您正在处理数字,则说明您已到达数字的末尾,应将其添加到输出中.

If the current character isn't a digit and you were in the middle of processing a number, you have reached the end of the number and should add it to your output.

这是一个简单的实现方式:

Here is a simple such implementation:

std::vector<int> read_integers(std::istream & input)
{
    std::vector<int> numbers;

    int number = 0;
    bool have_number = false;

    char c;

    // Loop until reading fails.
    while (input.get(c)) {
        if (c >= '0' && c <= '9') {
            // We have a digit.
            have_number = true;

            // Add the digit to the right of our number.  (No overflow check here!)
            number = number * 10 + (c - '0');
        } else if (have_number) {
            // It wasn't a digit and we started on a number, so we hit the end of it.
            numbers.push_back(number);
            have_number = false;
            number = 0;
        }
    }

    // Make sure if we ended with a number that we return it, too.
    if (have_number) { numbers.push_back(number); }

    return numbers;
}

(观看实时演示.)

现在,您可以执行以下操作以从标准输入中读取所有整数:

Now you can do something like this to read all integers from standard input:

std::vector<int> numbers = read_integers(std::cin);

这对于std::ifstream同样适用.

您可能会考虑将函数作为模板,其中参数指定要使用的数字类型-这将使您(例如)切换到long long int而无需更改函数,如果您知道文件将包含int内的大量数字.

You might consider making the function a template where the argument specifies the numeric type to use -- this will allow you to (for example) switch to long long int without altering the function, if you know the file is going to contain large numbers that don't fit inside of an int.

这篇关于从具有混合整数,字母和空格的文件中读取整数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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