Real simple question, regarding text file reading

Started by
12 comments, last by rip-off 15 years, 9 months ago
Sorry if you think this question is too easy to be on here, but this forum is the only one that I have a membership to. I have a text file which is this format: 3 2 3 4 -3 1 1 1 -3 0 1 0 -4 -5 -5 0 Basically, first line has 4 numbers (each representing a specific variable values - the first nubmer which is 3, represents the dimension of the matrix), then second line through 4th line is a matrix (3 by 3 matrix), then the last line is the right-hand-side of that matrix. Thus I should be able to solve this linear equations eventually. The problem comes from reading this file. At the moment I've got it so that: infile >> dim >> ns >> s >> d; //Reads the first line (4 variables) for (i = 1; i <= dim; i++) { for (j = 1; j <= dim; j++) { if (!(infile >> a[j])) { cout << "Couldn't read a[" << i << "][" << j << "]" << endl; exit(EXIT_FAILURE); } aa[j] = a[j]; } } This results in the exit always (end up with the "couldn't read a[1][1]" error message). I think the problem is that it doesn't go to the next line after reading the first 4 variables, and i'm sure there's an easy way of fixing this problem but my head is blank at the moment. Someone please help?
Advertisement
Could you post more code? A full, compilable example is often the best way to get help (feel free t cut out anything not related to the issue).
Well I've put up all the essentials pretty much, I just have a few more lines above:

int *indx, s, d, ns;
int num, job=0, job2=0;
double p, **aa ;
double *x;
double sum;
ifstream infile;
string line2;
char * inname = "matrix.txt";
infile.open(inname);

if (!infile) {
cout << "Cannot open file " << inname << " for reading." << endl;
exit(EXIT_FAILURE);
}

... and the stuff I put up previously comes here

The reason for having string line2; is because I do something similar to this in another function with string line;.
So I had to use line2 to avoid redefinition.
What is the type of "a"? Your have omitted the declarations of "a", "dim", "i" and "j". Please, paste a full source, and use [source][/source] tags to make a pretty box:
// like this!
If you were qualified to determine what was essential, you wouldn't have a problem. Post code. We've seen (and written) horrors you cannot imagine; you won't embarrass yourself. All you're doing is hindering our assistance.
Sorry about not using boxes... I just don't know how to do it.
I have to test it if i'm doing it right.
test
Okay now I got that to work... here is the full code.

void matcalc3 (){	//Variables	char read;	int i, j, k; 	int ctner=0;		ifstream inData;	string line;	char * name = "test.txt";  //Another file that is related to my work  	inData.open(name);	if (!inData) {        cerr << "Unable to open file";        exit(1);   // call system to stop	}	if (inData.is_open())	{		//Reads first line of the txt (#row, #col, #unknown) 		inData >> n;		inData >> m;		inData >> dim;...	    int *indx, s, d, ns;		int num, job=0, job2=0;		double  p, **aa ;		double *x;		double sum;		ifstream infile;		string line2;	    char * inname = "matrix.txt";		infile.open(inname);		if (!infile) {			cout << "Cannot open file " << inname  << " for reading." << endl;			exit(EXIT_FAILURE);		}		//cout << "File " << inname;		/*if (!(infile>>dim>>ns)){			cout <<  "\nFirst line must have size of system and number of rhs\n";			exit(EXIT_FAILURE);		}		if (ns < 1) {			cout << "\nMust have at least one right-hand side vector\n";			exit(EXIT_FAILURE);		}*/		/* make an "empty" loop to ignore the rest of the first line */		infile >> dim >> ns >> s >> d;		//while (infile.get() != '\n');    			indx = ivector(1, dim);      /* holds pivot info from ludcmp for lubskp*/		//a    = matrix(1, dim, 1, dim); /* this holds the original matrix         */		aa   = matrix(1, dim, 1, dim); /* a copy of the matrix to send to ludcmp */		//b    = vector(1, dim);       /* the original right-hand side           */		x    = vector(1, dim);       /* Copy rhs to x and send to lubksp       */		//cout << ": " << n << "x" << n << " matrix" << endl << endl;		//cout << scientific << showpos;		for (i=1; i<=dim; i++) {			for (j=1; j<=dim; j++) {				cout << "a[" << i <<"]["<<j<<"]="<<a[j]<<"\n";			}		}        		for (i = 1; i <= dim; i++) {			for (j = 1; j <= dim; j++) {				if (!(infile >> a[j])) {					cout << "Couldn't read a[" << i << "][" << j << "]" << endl;					exit(EXIT_FAILURE);				}				aa[j] = a[j];     /* save a, work with aa */				//cout << setw(15) << a[j]; //shows input matrix			}			//cout << endl;		}		ludcmp(aa, dim, indx, &p);         /* perform the decomposition */		for (num = 1; num <= ns; num++) { /* loop for the rhs vectors  */			//cout << "\n=============================================\n";			/* Solve equations for each right-hand vector */			for (k = 1; k <= dim; k++) {				/*if (!(infile >> b[k])) {					cout <<"Couldn't read b[" << k << "]" << endl;;					exit(EXIT_FAILURE);				}*/				x[k] = b[k];            /* save b, work with x            */			}			lubksb(aa, dim, indx, x);     /* back substitution for this rhs */			cout << "Solution:" << endl;  			for (j=0; j<=s-1; j++)			{    							for (i=1; i<=d; i++) //for (i=j*d+1; i<=d*(j+1); i++)				{					if (type[j*d+i]==0) {						cout << setw(10) << setprecision (3) << pres[j*d+i];					}					else if (type[j*d+i]==3) {						cout << setw(10) << " ";					}					else if (type[j*d+i]==2) {						job+=1;						cout << setw(10) << setprecision (3) << x[job];						pres[j*d+i] = x[job];					}					else if (type[j*d+i]==1) {						cout << setw(10) << setprecision (3) << pres[j*d+i]; //used to be just 'i'					}								}				cout << '\n';						}  			cout << endl << endl;			/*cout << "Right-hand side:" << endl;			for (i = 1; i <= n; i++) {				cout << setw(15) << b;			}			cout << endl;			cout << "Matrix * solution:" << endl;			for (i = 1; i <= n; i++) {				sum = 0.0;				for (j = 1; j <= n; j++) {					sum += a[j] * x[j];				}				cout << setw(15) << sum;			}*/		}		cout << "\n================================================================\n\n"; 		//Writing solution to txt          ofstream solfile ("solution.txt");          if (solfile.is_open())          {			 solfile << "Solution:" <<"\n";				for (j = 0; j <= s-1; j++)				{					for (i=1; i<=d; i++) //for (i=j*d+1; i<=d*(j+1); i++)					{						if (type[j*d+i]==0) {							solfile << setw(10) << setprecision (3) << pres[j*d+i];						}						else if (type[j*d+i]==3) {							solfile << setw(10) << " ";						}						else if (type[j*d+i]==2) {							job2+=1;							solfile << setw(10) << setprecision (3) << x[job2];						}						else if (type[j*d+i]==1) {							solfile << setw(10) << setprecision (3) << pres[j*d+i];						}					}					solfile << "\n";				}		  }    infile.close();    free_vector(b, 1, dim);    free_matrix(a, 1, dim, 1, dim);    free_matrix(aa, 1, dim, 1, dim);    free_vector(x, 1, dim);    free_ivector(indx, 1, dim);        cout << "Solution file written." << "\n";		//cout << "xcount = " << xcount << "\n";			} //eop

sorry for the inconvinience.
By the way,
"a" and "dim" are declared else where as:

double **a;
a = dmatrix(1, dim, 1, dim);
int dim;

same with some other variables if you happen to wonder where it's declaration is.
Program runs , it just that it exits due to the exit setup I have. (But ideal I wouldn't want that exit 'if' to satisfy.
What type is a? Is it a double pointer? How many doubles is it initialised to point at, given that you've commented out this line "a = matrix(1, dim, 1, dim);"?

If anyone is inconvenienced, it is you. By providing us with a compilable example, you give us everything we need to help you. I cannot compile your code, because I don't have the functions "matrix()" and "vector()". Therefore I cannot reproduce the problems you are having directly.

In any case, I think you should look carefully at your array indexing. Arrays in C++ are 0 based. In general, to loop N times, in C++ we write:
for( int i = 0 ; i < N ; ++i ){    // loop body}

You should be able to see that this loops the same number of times as i = 1 ; i <= N...
Sorry but it just is not possible for me to post the ENTIRE code, I've got 6 header files with each of them having at least 500 lines, and I also include GLUT and nrutil (numerical recipe) stuff. I suppose the vector(), matrix() bit comes from the numerical recipe headers.

The intention of my code is not to be able to read this file, this is only a small small part of what I'm trying to achieve.

I just need to know how to read the next line, after reading the first line.

Is it not possible to give me a tip regarding that without looking at my gazzilion lines of code?

This topic is closed to new replies.

Advertisement