Re: how to read tab delim file into 2D Array
yogi_bear_79 wrote:
Read about std::vector.
ok, I Have created a 2D vector, where c=num of columns in my file, and
r=num of rows. I've populated vectors from file before,using a while
loop and pushback, but never a 2D. Below is sample data, basically
the first row is useless, the second row starts with a null, then tab
tehn Exam 1, tab etc.
vector<string> v(c);
vector<vector<string> > v2(r,v)
This file contains the records of 20 students over 10 exams
Exam1 Exam2 Exam3 Exam4 Exam5 Exam6 Exam7 Exam8 Exam9 Exam10
Student1 90 100 89 75 88 99 100 87 88 85
Student2 99 89 87 89 87 90 87 99 100 95
This type of program is usually a homework assignment. As it most likely
is, we really aren't going to do your homework for you, but point you in the
right direciton until you at least attempt it and show some code.
Look at std::getline(), that will get a whole line of text.
You can then put this line into a stringstream and pull the data out.
As for a 2 dimentional dynamic array, look at
std::vector<std::vector<int> >
Something like std::vector<std::vector<int> > Data;
// for each student
Data.push_back( std::vector<int> );
// now push back the test scores
Look at the proper syntax to use for standard vector.
Another alternative is not to use a 2 dimentional array, but a structure or
class for each student that has it's own vector, then vector them.
something like:
struct Student
{
public:
std::string Name;
std::vector<int> TestScores;
};
then...
std::vector<Student> Data;
Now you push_back one Student to Data, then for that student populate the
std::vector. You could create a temp Student and populate it then push it
onto data, but I've never liked to copy vectors (which a push_back does,
makes a copy and puts it into the vector) when I dont have it. For this
trivial data it really shoudln't be a problem though.
Now, it's up to you to look at what everyone has said, decide how you want
to try it, and try it, When you get stuck then ask how to fix your code.
hth,
Jim
--
Jim Langston
tazmaster@rocketmail.com