Parsing Comma Separated Data File

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1224 Views - Last Post: 18 May 2013 - 07:11 PM Rate Topic: -----

#16 Rome_Leader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-May 13

Re: Parsing Comma Separated Data File

Posted 18 May 2013 - 06:04 PM

Sorry, maybe I wasn't clear. The number comes from the main function - this is just the implementation .cpp. In the main function, it's a const integer defined at compile time, but I do intend on changing it around for different sizes (currently attempting to test a 10x10). So in effect, the size is known.

Sucks that I can't edit my posts. :P By number, again to clarify, I mean the rows & columns. In the main function, both come as const integers.
Was This Post Helpful? 0
  • +
  • -

#17 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Parsing Comma Separated Data File

Posted 18 May 2013 - 06:31 PM

Then what is this in the function?

09	    int numberOfCols = 0;
10	    int numberOfRows = 0;

26	                ss2 >> numberOfCols;
27	                ss >> numberOfRows;





So you can allocate the array in a calling function?


The function head could be more like :

bool loadData(int **imgPix, int numberOfCols, int numberOfRows, string fileName);


Was This Post Helpful? 1
  • +
  • -

#18 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,234
  • Joined: 20-March 10

Re: Parsing Comma Separated Data File

Posted 18 May 2013 - 07:11 PM

While it all seems a bit unnecessary to use dynamic memory if you
know what the array size will be.

You might find this example based on your code to be useful

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

template <typename T> 
T **AllocateArray( int x, int y)
{
	T **dynamicArray;

	dynamicArray = new T*[x];
	for( int i = 0 ; i < x ; i++ )
		dynamicArray[i] = new T [y];

	return dynamicArray;
}

template <typename T>
void FreeArray(T** dArray)
{
	delete [] *dArray;
	delete [] dArray;
}


bool loadData(int **imgPix, string fileName) {
	
	ifstream inputFile;
	
	inputFile.open(fileName.c_str());
	
	
	int numberOfCols = 0;
	int numberOfRows = 0;
		
		
	if (!inputFile.is_open()) {
		return false;
	}
	
	
	while (inputFile)
  {
    string s;
    if (!getline( inputFile, s )) break;

    istringstream ss( s );
    

    while (ss)
    {
      string s;
      if (!getline( ss, s, ',' )) break;
	  istringstream ss2( s );
	  ss2>>imgPix[numberOfRows][numberOfCols];
	  numberOfCols++;
    }
	numberOfRows++;
    numberOfCols=0;
  }
	inputFile.close();
	return true;
}

int main()
{
	int **image;
	image=AllocateArray<int>(25,25);
	loadData(image,"test.txt");
	cout<< "\n Image info "<<image[2][2];
	cin.ignore();
	cin.get();
	FreeArray(image);
	return 0;
}



It uses templates to safely allocate memory and to deallocate it as well.

You might find other approaches to be more well, simpler if you know what the array size is going to be though.

Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2