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

reading a text file

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?

Nemoticchigga
Junior Poster in Training
98 posts since Feb 2008
Reputation Points: 10
Solved Threads: 2
 

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

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

how?

Nemoticchigga
Junior Poster in Training
98 posts since Feb 2008
Reputation Points: 10
Solved Threads: 2
 
how?

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

indata >> array[i][j];
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You