By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

Read 2d Array from Text File-

P: n/a
Hi-
I have read many posts with specific applications of reading in text
files into arrays, however I have not been able to successfully modify
any for my application. I want to take a text file filled with a tab
delimited list of 10 columns (floats) and read it into a 2D array.
The length of the columns are all the same, however this will be
variable from text file to text file. Any help (starter code or where
to read) would be much appreciated. Thanks-
-Ryan

May 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
RyanS09 wrote:
Hi-
I have read many posts with specific applications of reading in text
files into arrays, however I have not been able to successfully modify
any for my application. I want to take a text file filled with a tab
delimited list of 10 columns (floats) and read it into a 2D array.
The length of the columns are all the same, however this will be
variable from text file to text file. Any help (starter code or where
to read) would be much appreciated. Thanks-
-Ryan
Read them lines into a vector of vectors. Once read, you can extract
the elements and place them in a newly created 2d array if you want or
just keep the vector of vectors around.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 29 '07 #2

P: n/a
"RyanS09" <Ry*****@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
Hi-
I have read many posts with specific applications of reading in text
files into arrays, however I have not been able to successfully modify
any for my application. I want to take a text file filled with a tab
delimited list of 10 columns (floats) and read it into a 2D array.
The length of the columns are all the same, however this will be
variable from text file to text file. Any help (starter code or where
to read) would be much appreciated. Thanks-
This is somewhat trivial and may be homework. There are a few ways to do
it, I decided to go ahead and code it up the way I would do it. Others may
use different methods ( InputFile >SomeFloat instead of a string, for
example).

Code was tested and ran with a simple data file of 10 floating point values
per line. It displayed the appropriate error message if a line had too many
or too less values. A possible optimization (which I would probably do if
this was in production) would be to push an empty vector<floatto data,
grab an iterator to it and push the data into that, rather than pushing the
full vector which requires a copy of the values. Pre-sizing the vector may
help, but I think that 10 floats is less than the default allotment anyway
(didn't bother to test since I'm not going to be using this code).

#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>

static const int LineLength = 10;

void Pause()
{
std::string Wait;
std::getline( std::cin, Wait );
}

int main()
{
std::ifstream InputFile( "testinput.txt" );
if ( ! InputFile.is_open() )
{
std::cout << "Error opening testinput.txt!" << std::endl;
Pause();
return 1;
}

std::vector< std::vector< float Data;

std::string Input;
while ( std::getline( InputFile, Input ) )
{
// Input contains entire line
std::stringstream Parse;
Parse << Input;
float Value;
std::vector<floatLine;
while ( Parse >Value )
Line.push_back( Value );
if ( Line.size() != LineLength )
{
std::cout << "Line " << Data.size() + 1 << " expected " <<
LineLength << " values, received " << Line.size() << " values, aborting." <<
std::endl;
Pause();
return 1;
}
Data.push_back( Line );
}

// close not specifically needed, will be called by destructor when
// ifstream goes out of scope, but I always call it anyway.
InputFile.close();
// At this point, all read values are in our vector of vector of floats
named Data.
std::cout << "Data:\n";
for ( size_t i = 0; i < Data.size(); ++i )
{
for ( size_t j = 0; j < Data[i].size(); ++j )
std::cout << Data[i][j] << " ";
std::cout << "\n";
}

Pause();

return 0;
}

May 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.