473,412 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

overloading operator >> to read a text file into a Matrix

I have
template <typename T>
inline std::istream& operator >> (std::istream& is, Matrix<T>& mIn)
{
int rows,cols;
is.clear();
is >> rows >> cols;
mIn._M=rows;
mIn._N=cols;
mIn.MatAlloc(rows,cols);//alllocate mem

for (int i=0;i<rows;i++)
for (int j=0;j<cols; j++)
if(is.good())
is >> mIn(i,j);

return is;
}
This works fine if i have a text file such as
2 3 //2 rows 3 cols
1 2 3
4 5 6

but if i have something like
2 3
1 2
I get a matrix M with the following data member
2 3
1 2 2
0 0 0

notice how M(0,1)==M(0,2) ==2 which i DONT want. Any idea?

Sep 14 '05 #1
1 3091
<bl**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have
template <typename T>
inline std::istream& operator >> (std::istream& is, Matrix<T>& mIn)
{
int rows,cols;
is.clear();
is >> rows >> cols;
mIn._M=rows;
mIn._N=cols;
mIn.MatAlloc(rows,cols);//alllocate mem

for (int i=0;i<rows;i++)
for (int j=0;j<cols; j++)
if(is.good())
is >> mIn(i,j);


is will be "not good" only after attempting to read. Try this:

for (int i=0;i<rows;i++)
for (int j=0;j<cols; j++)
{
int value;
if (is >> value)
{
mIn(i, j) = value;
}
}

Ali

Sep 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
6
by: Michael Hopkins | last post by:
Hi all I have a class that contains as one of it's members a type that is internally constructed like this: std::vector< std::valarray< float > > foo Think of it as a matrix. I cannot...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
4
by: Ook | last post by:
I'm a but fuzzy about this. You would call it like this:Polynomial poly; // Polynomial is my class with various properties cin >> poly; In the function itself, I would, for example, do the...
1
by: Suresh Tri | last post by:
Hi, I was trying to overload concat operator ||(text,text) such a way that it behaves like Oracle. i.e. I want 'abc' || null to return 'abc' instead of null. I know that it is not the expected ...
6
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
11
by: Ashwin | last post by:
hi guys, i have overloaded >operator for my own string class .the function is as given below istream& operator>>(istream &in,string1 &s) { char *a; a=new char; in>>a; s=a;
2
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.