I am attempting to read a text file.

ifstream indata;
	                indata.open(filename);
		if(!indata) 
		{ 
			// file couldn't be opened
		}
		else
		{
			// file opened
		}

		// keep reading until end-of-file
		string data;

		while (getline(indata, data)) 
		{
			//this is where i want to store values
		}
	
		indata.close();

I want to read values from a text file. Im making the text file so I can read it however. This is a simple example of what I was thinking:

////text file/////
1 2 3 4
5 6 7 8
9 10 11 12
////text file/////

How do I store these values into a 2 dimensional array?

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

If there is no need for error checking I would read the values from the file as integers as opposed to strings.

how?

Set up your 2-D array of integers, then read values into the array with the >> operator.

indata >> array[i][j];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.