将逗号分隔的文件读入整数数组 [英] Reading a comma separated file into an integer array

查看:260
本文介绍了将逗号分隔的文件读入整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将包含整数的文本文件读取到整数数组中。
如果输入为:1 3 4 5 6
(之间有空格)可以正常工作。

Iam trying to read a text file containing integers into an integer array. If the Input is: 1 3 4 5 6 (with space in between) It is working fine.

但如果输入为: 1,3,4,5,6(以逗号分隔)。它仅打印1.(第一个数字)。如果程序将1,3,4,5,6作为单个实体,则应打印
1 ,3,4,5,6作为第一个指数ryt?
还有File >> x,该表达式是否通过检测两者之间的空格来逐个取值?

but if the input is: 1,3,4,5,6 (comma separated) .Its just printing 1.(first digit).If the program finds 1,3,4,5,6 as a single entity then it should print 1,3,4,5,6 as the first index ryt? And also File>>x , does this expression take value one by one by detecting the space in between??

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{    
    int n = 0; //n is the number of the integers in the file ==> 12
    int num;
    int arr[100];
    int x;
    int sum = 0;
    ifstream File;
    File.open("integer.txt");
    if(!File.is_open())
    {
        cout<<"It failed"<<endl;
        return 0;
    }

    while(File>>x)
    {
        arr[n] = x; 
        n++;
    }

    File.close();
    cout<<"n : "<<n<<endl;
    for(int i=0;i<n;i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}


推荐答案


但如果输入为:1,3,4,5,6(逗号分隔)。它仅打印
1.(第一位)。如果程序找到1,3,4,5,6作为单个实体,则应打印1,3,4,5,6作为第一个索引ryt?

but if the input is: 1,3,4,5,6 (comma separated) .Its just printing 1.(first digit). If the program finds 1,3,4,5,6 as a single entity then it should print 1,3,4,5,6 as the first index ryt?

它只打印 1,因为您尝试将 1、3、4、5、6读入 int 对象。但是, int 不能为 1,3,4,5,6。简而言之,解析会在到达第一个坏字符(即逗号)后立即停止,并且最终会得到到目前为止已建立的整数(即 1)。

It prints just "1" because you attempt to read "1,3,4,5,6" into an int object. An int, however, cannot be "1,3,4,5,6". Plainly speaking, parsing stops as soon as the first "bad" character, i.e. the comma, is reached, and you end up with the integer number that has been built up so far, i.e. "1".

其余的输入将被丢弃。就像您的行是 1abcdef或 1abcdef2345一样。

The rest of the input is discarded. It's as if your line was "1abcdef", or "1abcdef2345".


还有File >> x,此表达式的值是否为1

And also File>>x , does this expression take value one by one by detecting the space in between??

是的,这使它变得非常不灵活。

Yes, and that makes it quite inflexible.

我建议使用的是 operator>> 。 com / w / cpp / string / basic_string / getline rel = nofollow noreferrer> std :: getline ,并通过', '作为分隔符。虽然该函数的名称不再有意义,因为它不再读取 lines (就像使用默认定界符'\n'一样) ,它就可以正常工作。

What I recommend instead of fiddling with operator>> is using std::getline, passing ',' as delimiter. While the function's name then no longer makes sense, because it no longer reads lines (as it would with the default delimiter '\n'), it will work just fine.

最后,您会得到一个简单的 std :: string 对象使用int > std :: stoi

You will end with up with individual std::string objects which are easy to convert to ints using std::stoi.

在使用它时,请摆脱原始 int arr [100] 并将其设置为 std :: vector< int> ,这样您就不仅限于100个元素。无论如何,100是一个丑陋的魔术(任意)数字。

While you're at it, get rid of the raw int arr[100] and make it a std::vector<int>, so that you are not limited to 100 elements. 100 is an ugly magic (arbitrary) number, anyway.

这里是一个示例:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    // faking some test file input; behaves like an `std::ifstream`:
    std::istringstream is("1,2,3,4,5");

    std::vector<int> numbers;

    std::string number_as_string;
    while (std::getline(is, number_as_string, ','))
    {
        numbers.push_back(std::stoi(number_as_string));
    }

    std::cout << "n : " << numbers.size() << "\n";
    for(auto&& number : numbers)
    {
        std::cout << number << "\n";
    }
}

您也可以看到,有机会提出一些其他好的C ++做法,例如避免使用使用命名空间std std :: endl

As you can see, I've also taken the chance to propose some other good C++ practices, such as avoiding using namespace std and std::endl.

这篇关于将逗号分隔的文件读入整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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