954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help getting txt data into a 2D array with multiple delimiters

I have data in a txt file named Test2.txt that looks like this.

1a,1b,1c,1d,1e
2a,2b,2c,2d,2e
3a,3b,3c,3d,3e
4a,4b,4c,4d,4e
5a,5b,5c,5d,5e

I want to put this data into a 2D array so that dataArray[0][0] will hold 1a, dataArray[0][1] will hold 1b, dataArray[1][0] will hold 2a, etc.

However, the problem is that I need to parce my data by a return stroke and a comma. When I do the getline functions, I can parce it by returns without trouble, but parcing by a comma doesn't work like I want it to. In the end, I end up assigning an entire comma delimited line to one array index. Here is my code. (I hope I put this in right, it's my first time posting).

<code>#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
using namespace std;

int main()
{
	const int ROW = 5;
	const int COL = 7;
	int i,j;
	string fileLine;
	string fileData;
	string dataArray[ROW][COL];
	
                ifstream inFile;						
	inFile.open(&quot;Test2.txt&quot;);					

    if(inFile)
	{
		while(!inFile.eof())	
		{
			getline(inFile,fileLine);
			stringstream iss;
			iss &lt;&lt; fileLine;
			for(i=1;i&lt;ROW;i++)
			{
				for(j=1;j&lt;COL;j++)
				{
					while(getline(iss,fileData,','))
					{
						dataArray[i][j] = fileData;
						cout &lt;&lt; dataArray[i][j] &lt;&lt; endl;
					}
				}
			}
			iss.clear();
		}
	}
	else
	{
		cout &lt;&lt; &quot;Error in opening file&quot; &lt;&lt; endl;
	}

	inFile.close();	

	system(&quot;pause&quot;);
	
	return 0;
}</code>


Now the output for this looks like it works because it lists 1a through 7e. However, these weren't assigned to the correct array indices.

I'd appreciate any tips anyone has, even if it means taking another approach in reaching my goal.

Bennymin
Newbie Poster
2 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
   ifstream file("Test2.txt");              
   if ( file )
   {
      const int ROW = 5, COL = 7;
      string array[ROW][COL];
      for ( int i = 0; i < ROW; i++ )
      {
         string line;
         if ( getline(file, line) )
         {
            stringstream iss(line);
            for ( int j = 0; j < COL; j++ )
            {
               if ( getline(iss, array[i][j], ',') )
               {
                  cout << "array[" << i << "][" << j << "] = " 
                       << array[i][j] << '\n';
               }
            }
         }
      }
   }
   else
   {
      cout << "Error in opening file" << endl;
   }
   return 0;
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks Dave, that worked well.

Bennymin
Newbie Poster
2 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You