Read 2d Array from Text File-

R

RyanS09

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
 
V

Victor Bazarov

RyanS09 said:
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
 
J

Jim Langston

RyanS09 said:
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<float> to 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<float> Line;
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.size(); ++j )
std::cout << Data[j] << " ";
std::cout << "\n";
}

Pause();

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top