Text file reading problem
| pkiskool (6) | |||
| I've got a problem regarding reading a text file. 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: 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); } 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[i][j])) { cout << "Couldn't read a[" << i << "][" << j << "]" << endl; exit(EXIT_FAILURE); } aa[i][j] = a[i][j]; } } . . . other stuff . . . 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? | |||
| Zaita (1560) | |||
| Where is A declared? Have you allocated memory for it? | |||
| pkiskool (6) | |||
| It is declared else where as: double **a; a = dmatrix(1, dim, 1, dim); | |||
| Mitsakos (343) | |||
| But you read from the file to dim value here. You can't use the dim before it... Also you didn't allocate any memory for **aa | |||
| pkiskool (6) | |||
| Oh it is read again in "matrix.txt" file... The thing is that dim value updates as it is looped. Thus I need to read in new updated-dim value everytime. It is read first in a another file called "contour.txt", then it uses that dim value to decalre "a". Then this function I put up above comes in later (with new dim value). | |||
| pkiskool (6) | |||
| Sorry, I actually do but just left it out. [source] indx = ivector(1, dim); /* holds pivot info from ludcmp for lubskp aa = matrix(1, dim, 1, dim); /* a copy of the matrix to send to ludcmp x = vector(1, dim); /* Copy rhs to x and send to lubksp */ [/source] The code is really lengthy so thus I didn't bother putting up the whole thing. | |||
| Mitsakos (343) | |||
| I don't know about matrix... I can't find anywhere the dmatrix() function... I'll post an example on how to do the same thing with new() instead of dmatrix():
You can put together the first to for-loops so you won't do the same loop twice. Anybody who knows better about it can help you more. This is just how I would do it. | |||
| pkiskool (6) | |||
| Thanks, I'll see what I can do with this. It's end of the day so I might try it out tomorrow morning. Could you check back again around tomorrow noon to see if I have any question? I'll let you know how it went. Thanks. | |||
| Mitsakos (343) | |||
| sure, no problem | |||
| shereif102 (4) | |||
| i think the problem is if (!infile) { cout << "Cannot open file " << inname << " for reading." << endl; exit(EXIT_FAILURE); } i think infile is an object so it will not = false or null :D and as u said the program allows extining ithink that is the error | |||
| pkiskool (6) | |||
| I think you are absolutely right shereif102... infile is an object! and thanks to you as well Mitsakos , I think your approach seems to be working. Thanks for the help everyone, despite me putting up insufficient information regarding the code. | |||
| Mitsakos (343) | |||
The if(!infile) works fine. But if you want to be completely accurate you can use:
if(!infile.is_open()){}The is_open() returns true if the file is open and false otherwise. [EDIT] The infile is a stream. When trying to open the file it will have some contents (different than 0) so it will act as a True in the if. If it returns an error then it is gonna have the value of 0 which is also a value for the False value. | |||
This topic is archived - New replies not allowed.
