c++ - Loading Data From Comma Separated Text File to 2D Array? -


i trying load data text file looks this:

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

etc. have functions manipulate it, , ensured array correct size (which may still vary). below attempt @ implementation:

bool loaddata(int **imgpix, string filename) {  ifstream inputfile;  inputfile.open(filename.c_str());  string templinerow; //the resulting line 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 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 line double pointer assignment comment, because believe source of error, although there others. i'm not sure how use while loop structure i've implemented iteratively update 2d array.

can offer assistance? appreciated!

imgpix = new int* [numberofrows] 

here numberofrows = 0, don't allocate enough memory. need know dimensions of array before allocate memory. , should allocate memory each row in array:

imgpix[currentrow] = new int [totalcols]; 

for 2-dimensional rectangular array, more efficient create 1-dimensional array of totalrows*totalcols elements, , access using formula a(row, col) = a[row*totalcols + col].


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -