Thread: iostream manipulators

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    iostream manipulators

    let's say i have a data file i want to write:
    Code:
    float gpa=3.45654;
    int cost=45430;
    string name;
    name = "two words"
    ofstream out("out.file");
    out << gpa << cost << name;
    out.close();
    
    /* .... */
    
    ifstream in("out.file");
    in >> gpa >> cost >> name;
    in.close();
    most of you can see the problem here: name has a space in it. what i want to do is seperate each field with a comma (or some other distinct marker). is there a manipulator that can automatically end everything with a character of my choice?

    //edit: for instance:
    out << gpa << cost << name;
    should output
    3.45654,45430,two words

    and input should act like this:
    in >> gpa >> cost >> name;
    it should take a float, throw away the comma, get the integer, throw away the comma, then get the whole string, regardless of spaces, but only until it hits a comma (or the end of the string).

    theoretically, anyway. i don't know if this is possible even with changing the iostream and/or the string classes
    Last edited by ygfperson; 07-18-2002 at 01:25 PM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    look at overloading the '>>' and '<<' operators to do what you want.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    I'm not sure you really want operator>>/<< but you could try something llike -

    Code:
    #include <iostream>
    #include <string>	
    #include <sstream>
    using namespace std;
    
    
    istream& operator>>(istream&  in,string& s)
    {
    	char c;
    	c = in.get();
    	if(c!=' ')
    		in.putback(c);
    	return getline(in,s,',');
    }
    
    
    int main()
    {
    	string abc = "3.45654 45430 two words,";
    	stringstream ss;
    	float a;
    	int b;
    	string c;
    	ss << abc;
    	ss >> a >> b >> c;
    
    	cout << a << ' '<< b << ' ' << c;
    
    
    	return 0;
    }
    This would rely on your file having a set format of float-int-string with a comma delimiting the end.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i didn't know you could overload that operator like that. i always thought that it was already defined so i would have to use a manipulator or something.

    thanks. but one more thing. in my code i want to store a file looking like:
    Code:
    35,3.4,the next time,85,8.9,zero,...
    where each field is seperated by a comma, and the fields are float, int, and string in order, repeating until eof. could i still overload the >> and << operators to do this?

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    yes. All you have to do is write an equivalent function that would do the same thing and overload the operator with that.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    say i wanted to return to the behavior of the iostream before hand after i'm done inputting and outputting the file. what can i do?

  7. #7
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    I dont think there's an "automatic" manipulator of that kind, maybe the getline() for reading purposes esp. in case of char/strings but i dont know of any function for output.

    and if you put a comma to seperate then you'll have to code your own function to throw/leave the comma when it encounters them.
    -

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    317
    That's the inherent beauty with operator overloading, you make the operators 'smart'. So lets say all your data was a struct called mydata for instance, then you woudl overload the '<<' operator like so:

    Code:
    ofstream& operator<<(ofstream& , mystruct& ){
                    /*then the code in here will only be executed when the above conditions exist.  That is there is an 
    object of type ofstream on the left and an object of type mystruct on the right*/
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drieving from iostream
    By Thantos in forum C++ Programming
    Replies: 8
    Last Post: 06-23-2004, 04:57 PM
  2. iostream & hex?
    By dmlx90 in forum C++ Programming
    Replies: 0
    Last Post: 05-22-2002, 11:51 PM
  3. DJGPP Doesn't include IOSTREAM!!!!!!!!!!!
    By Frenchfry164 in forum Game Programming
    Replies: 12
    Last Post: 10-27-2001, 12:27 PM
  4. << in iostream
    By badman in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2001, 10:19 PM
  5. Is there a C iostream?
    By Khisanth in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 12:34 AM