how to read tab delim file into 2D Array

Y

yogi_bear_79

I have a tab delimited file, that I want to read into a 2D array. I
want to ignore the first two lines and the first colum of data.
 
S

Sashi Asokarajan

yogi_bear_79 said:
I have a tab delimited file, that I want to read into a 2D array. I
want to ignore the first two lines and the first colum of data.
I would probably read the line with std::ifstream.getline() using '\n' as delimiter
into a std::string buffer and then split the string using std::stringstream.getline()
using '\t' as delimiter.

sashi
 
O

Obnoxious User

Ok, I can do that, but don't I have to declare the size of the array
before I populate it? I have written the code to figure out how many
rows and columns the text file contains. But I can not figure out a way
to declare the array with non-constant variables?

Read about std::vector.
 
Y

yogi_bear_79

I would probably read the line with std::ifstream.getline() using '\n' as delimiter
into a std::string buffer and then split the string using std::stringstream.getline()
using '\t' as delimiter.


Ok, I can do that, but don't I have to declare the size of the array
before I populate it? I have written the code to figure out how many
rows and columns the text file contains. But I can not figure out a
way to declare the array with non-constant variables?
 
T

Thomas J. Gritzan

yogi_bear_79 said:
Ok, I can do that, but don't I have to declare the size of the array
before I populate it? I have written the code to figure out how many
rows and columns the text file contains. But I can not figure out a
way to declare the array with non-constant variables?

Use std::vector<> or similar, so you can add new rows, columns.
 
Y

yogi_bear_79

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
 
J

Jim Langston

yogi_bear_79 said:
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
 
Y

yogi_bear_79

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.


I'm sorry if it was assumed I was looking for someone to do it for me.
This is what I have thus far. Two problems. The code below skips the
last column in each row. Also I still don't see how to add the data
into the 2D array.

int main()
{
ifstream file;
string line;
std::vector<std::vector<int> > Data;
string value, delim = "\t";

file.open ("test.txt",ios::in);
assert(file.is_open());

while (!file.eof()){
std::getline(file,line);
size_t startPos = 0, pos = line.find(delim);
while (pos != std::string::npos){
value = line.substr(startPos, pos - startPos);

//Data.push_back( std::vector<int> );
cout << value << endl;

startPos = pos + delim.length();
pos = line.find(delim, startPos);
}
}

file.close();
}
 
Y

yogi_bear_79

Getting Closer: This is building the vector, but it is one-
dimensional, seems to be a series of rows, versus columns. For
example the data is stored in d[0][0] - d[219][0]. Whenever I attempt
to increment col I get runtime errors. Also still need to figure out
how to use row to get to the next row in the vector, and finally I am
still losing my last column of data. Still Working on it though.
Tips needed!

int main()
{
ifstream file;
std::string line, value, delim = "\t";
typedef std::vector<std::vector<string> > Data;
Data d;
int row = 0, col = 0;

file.open ("test.txt",ios::in);
assert(file.is_open());

while (!file.eof()){
std::getline(file,line);
size_t startPos = 0, pos = line.find(delim);
if(row > 1)//skip title & header row
while (pos != std::string::npos){
value = line.substr(startPos, pos - startPos);
d.push_back(std::vector<string>(1));
d.back().at(col)=value;
startPos = pos + delim.length();
pos = line.find(delim, startPos);
}
row++;
col = 0;
}

file.close();
 
Y

yogi_bear_79

Eureka! After my 7th or 8th tutorial website, I landed on one that
explained it well. This is my current code, working so far..

int main()
{
std::ifstream file( "test.txt" );
std::string line, value;
std::vector<std::vector<string> > Data;
std::vector<string> Line;
std::stringstream Parse;

assert(file.is_open());

while (std::getline(file, line)){
Parse << line;
while ( Parse >> value )
Line.push_back( value );
Data.push_back( Line );
}
file.close();
}
 

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,007
Latest member
obedient dusk

Latest Threads

Top