multiple file reads to get values for linked lists(matrices)

Hey guys, I'm trying to debug a big project i have and I think I've encountered the first problem. right now the problem lies with getting list values from textfiles by going through them more than once and when i output the values, I get nothing.

A textfile has 3 columns that represent a matrix

example:

row column value
1 1 1
1 2 2
2 1 1
2 2 2

My code looks long but its really the same thing getting repeated over and over again.
This is the code portion I'm trying to fix:

#include<iostream>
#include<fstream>
#include<sstream>
#include<cstring>
#include<string>

using namespace std;

struct row
{
double value;
row *row_next;
row(double val1, row *next1=NULL)
{
value=val1;
row_next=next1;
}

};

struct column
{
double value;
column *column_next;
column(double val1, column *next1=NULL)
{
value=val1;
column_next=next1;
}
};

int main()
{
row *m_row = NULL;
column *m_column = NULL;
row *RPTR = m_row;
column *CPTR = m_column;
row *R_GARBAGE = m_row;
column *C_GARBAGE = m_column;

string a[3];
string b[3];
int i=0;
int j;
int x, y;
int r, c, val;
int rows1=0, rows2=0, columns1=0, columns2=0;
double holder=0;
string temp;
string temp2;

ifstream textfile1("a.txt");
ifstream textfile2("b.txt");
ofstream textfile3("c.txt");



//Finds number of rows and columns in file 1.
while(!textfile1.eof())
{
while(getline(textfile1, temp))
{
if(temp.find_first_not_of("0123456789")== std::string::npos)
{
continue;
}
else
{
break;
}
}

stringstream str(temp);
while(str >> a[i])
{
i++;
}
r = atoi(a[0].c_str());
c = atoi(a[1].c_str());
val = atof(a[2].c_str());

if(r>=rows1) rows1=r;
if(c>=columns1) columns1=c;
i=0;
}


//Finds number of rows and columns in file 2.
while(!textfile2.eof())
{
while(getline(textfile2, temp2))
{
if(temp.find_first_not_of("0123456789")==std::string::npos)
{
continue;
}
else
{
break;
}
}

stringstream str2(temp2);
while(str2 >> b[i])
{
i++;
}
r = atoi(b[0].c_str());
c = atoi(b[1].c_str());
val = atof(b[2].c_str());

i=0;
if(r>=rows2) rows2=r;
if(c>=columns2) columns2=c;
}

x=1;
y=1;

while(x<rows1+1)
{
while(!textfile1.eof())
{
//Gets valid line from file 1
while(getline(textfile1, temp))
{
if(temp.find_first_not_of("0123456789")==std::string::npos)
{
continue;
}
else
{
break;
}
}

//Stores row, column, and value int a[3]
stringstream str(temp);
while(str >> a[i])
{
i++;
}
r = atoi(a[0].c_str());
c = atoi(a[1].c_str());
val = atof(a[2].c_str());
i=0;

//Supposed to store val into list if r is equal to x
if(r==x)
{
m_row = new row(val, m_row);
}
}
//increments x for next iteration
x++;
}

RPTR = m_row; // sets RPTR equal to m_row linked list

//Should output m_row values but doesn't
while(RPTR != NULL)
{
cout << RPTR->value << " ";
RPTR = RPTR->row_next;
}

system("pause");
return 0;
}

Thanks,
jktexas1
I would suggest using classes, and instead of using 3 files for one matrix use one file for one matrix.
Or just use a 2D array with nested for loops.
Topic archived. No new replies allowed.