简体   繁体   English

将数据从逗号分隔的文本文件加载到2D数组?

[英]Loading Data From Comma Separated Text File to 2D Array?

I am trying to load data from a text file that looks like this: 我正在尝试从如下所示的文本文件中加载数据:

161,77,88,255 0,44,33,11,111 161,77,88,255 0,44,33,11,111

etc. I have functions to manipulate it, and am ensured that the array is the correct size (which may still vary). 等等。我具有操纵它的功能,并确保该数组的大小正确(可能会有所不同)。 Below is my attempt at implementation: 以下是我的实现尝试:

bool loadData(int **imgPix, string fileName) {

ifstream inputFile;

inputFile.open(fileName.c_str());

string tempLineRow; //The resulting line from the text file
string tempElementColumn; //The individual integer element 
int numberOfCols = 0;
int numberOfRows = 0;


if (!inputFile.is_open()) {
    return false;
}

imgPix = new int* [numberOfRows];

    while (getline(inputFile, tempLineRow, '\n')) {

        stringstream ss; 
        ss << tempLineRow; //Stringstream version of the line

        while (getline(ss, tempElementColumn, ',' )) {
            stringstream ss2; 
            ss2 << tempElementColumn;
            ss2 >> numberOfCols;

//Prob?         (**imgPix) = *(*(imgPix + numberOfRows) + numberOfCols);
            numberOfCols++;

        }

        numberOfRows++;

    }   

inputFile.close();
return true;

} }

I've marked the line with the double pointer assignment with a comment, because I believe it be the source of my error, although there could be others. 我用注释将双指针分配给了这一行,因为我相信这是我的错误的根源,尽管可能还有其他错误。 I'm not sure how to use the while loop structure I've implemented to iteratively update the 2D array. 我不确定如何使用已实现的while循环结构来迭代更新2D数组。

Can anyone offer any assistance? 谁能提供帮助? Would be greatly appreciated! 将不胜感激!

Your code has several issues, mostly I guess because you did not fully understand how built-in arrays work in C++. 您的代码有几个问题,主要是我想是因为您不完全了解内置数组在C ++中的工作方式。 The main issue here is that those cannot easily grow dynamically, there is no "resize" operation for those arrays (but you will need one here). 这里的主要问题是那些数组不能轻易地动态增长,这些数组没有“调整大小”操作(但是这里需要一个)。 So I suggest kindly that you try to rewrite your code using std::vector<int> and std::vector< std::vector<int> > , make sure you use of .resize when you store a new row or column and come back and ask again when you run into problems with this new implementation. 因此,我建议您尝试使用std::vector<int>std::vector< std::vector<int> >重写代码,并确保在存储新行或新列时使用.resize并请回来再询问您何时遇到此新实施的问题。

imgPix = new int* [numberOfRows]

Here numberOfRows = 0, so you don't allocate enough memory. 这里numberOfRows = 0,所以您没有分配足够的内存。 You need to know dimensions of the array before you allocate the memory. 在分配内存之前,您需要了解阵列的尺寸。 And then you should also allocate a memory for each row in the array: 然后,您还应该为数组中的每一行分配一个内存:

imgPix[currentRow] = new int [TotalCols];

For a 2-dimensional rectangular array, it would be more efficient to create 1-dimensional array of TotalRows*TotalCols elements, and then access it using formula A(row, col) = A[row*TotalCols + col] . 对于二维矩形数组,创建TotalRows * TotalCols元素的一维数组,然后使用公式A(row, col) = A[row*TotalCols + col]进行访问将更为有效。

先前的答案是有效的,但是如果不需要连续内存,则在这种情况下,与std::vector相比, std::deque s可能是更好的选择,以避免大量的内存重新分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM