skipping whitespace with istream

This is a discussion on skipping whitespace with istream within the C++ Programming forums, part of the General Programming Boards category; hello, i have a matrix, and i need to save its contents to a file, and then load it back ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    skipping whitespace with istream

    hello,

    i have a matrix, and i need to save its contents to a file, and then load it back from the file.
    i have the following code:

    Code:
    template<class T>
    void Matrix<T>::write(ostream& os) const {
        for(int i=0;i<line;i++){
    		for(int j=0;j<col;j++){
    			os << data[i][j] << " ";
    		}
    	}
    }
    
    template<class T>
    void Matrix<T>::read(istream& is) {
        for(int i=0;i<line;i++){
    		for(int j=0;j<col;j++){
    			is >> data[i][j];
    		}
    	}
    }
    so my file should look like this for example: 1 2 3 4 5
    when i'm trying to load it back its not working, so i was thinking that maybe the whitespace characters are messing it up.. so how do i ignore them in the read function?

    thanks.

    A.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    The >> operator already skips whitespace. Your code looks fine.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    unfortunately when i write() it, then fill the matrix with zeros, then load it back using read() and then list it out again, its full of zeroes..

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Quote Originally Posted by Avraham View Post
    unfortunately when i write() it, then fill the matrix with zeros, then load it back using read() and then list it out again, its full of zeroes..
    You'll have to show more of the surrounding code, because I don't see anything wrong in this part of it.

    EDIT: You don't happen to be reading and writing from the same fstream object, are you?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Code:
    #include "matrix.cpp"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
    	Matrix<int> matrix2(3,3);
    	matrix2.fill();
    	matrix2.list();
    	ofstream ofs("Matrix.txt");
    	matrix2.write(ofs);
    	matrix2.zero();
    	matrix2.list();
    	ifstream ifs("Matrix.txt");
    	matrix2.read(ifs);
    	matrix2.list();
        return 0;
    }
    this is my main.cpp, am i doing it wrong?

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You should close your output file before using it as input again.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    its working!!! thank you guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istream
    By Beowolf in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2007, 06:11 PM
  2. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. istream >> overloading
    By wazza13 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 11:56 PM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21