hi guys i am having a trouble to read multiple columns of data from file. for e.g.
2
3 4
5 6 8
9 0 1 5 9
4 3 1
6 9 0 2...
i am trying to read this file and put the first two in two arrays and multiply them and put the result in the third array. and multiply third to the result and so on.
any ideas will be appreciated. thank you guys.
I'm not sure if I follow you, could you rephrase your question more clearly?
You want to put the first 2 columns in 2 arrays and multiply them? multiply them by what? each other?
and multiply third to the result
third what? the third array in your program or third column from your file?
If I understand you correctly it would be possible to put the entire file into a matrix, and then transpose the matrix. You could then use ordinary fstream operations to read from the matrix from left to right. You would still have to format the text somehow to get this to work. Other than that the only way I can think of is keeping track of where you left off from in each row and using seekg() to go back to it after you have reached the end of the column etc.
If this sounds too complex there are free implementations of matrix classes on the net. google "apmatrix.h apmatrix.cpp" for example. You will also need apvector.h and apvector.cpp in order to compile. However apmatrix does not support a transpose function, but there are numerous matrix transpose functions posted on the net. You can edit apmatrix.h & .cpp to include one of these functions.
This however may be even more complex than using seekg(). Up to you.
sorry guys to make you confuse. the question is that i have file where values are not in order i have given examples in above thread.the things is that i want to put values from the file in arrays. like first row in first array and second row in second array and multiply them together and put the result to the third array and then i need to read another row (i.e third row) from the file and put it in the first array and the fourth row in the second array (the value in the first array and second array need to replace by the third row data and fourth row data respectively) and again multiply it together and add the result with the value in the third array. i need to keep doing this until end of file.
here i have posted my program that i have done so far. it's hasn't finish yet.
Something I don't really like about C++ iostream and stringstream related classes is it is not so intuitive as compared to what we are familiar with those format specifiers in sscanf, fscanf, scanf, printf, fprintf etc. Maybe this view is subjective but I feel format specifier to be more intuitive. %d is integer, %f is float, %.2f is 2 decimal place, %s is string etc etc.
Anyone share my view ? :P
Edit: Btw feof has some counter-intuitive behavoir so I think instream.eof is same. We should use getline in the while