954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Write to Screen and File{I-O question}

Hi,

i have five{number is unimportant} independent objects{the purpose is to model sensors} and i want them to write sequentially{not in the same time}, some info to a file{all to the same file} and at screen...

i was thinking to implement a fileWriter class, that will create the file...and then pass a reference {of an object of fileWriter class} to each of the five objects-sensors, so they can write to the same file.

The problem is that i dont know if this is the best way to do this{object oriented speaking} and if this is the way how can i implement this system similar to cout?
In particular someone can use cout anywhere in his program{if he includes iostream} and always the output is the same{stdout}....

Last question::does anyone have a good link for input-output in c++?

thanks for your time,
N.A

n.aggel
Posting Whiz in Training
203 posts since Nov 2006
Reputation Points: 23
Solved Threads: 12
 

Hmm... perhaps you want to overload the behaviour of << ? I'd say create a base class <strong>CFileWrite</strong> which implements member functions which print to a file and to the screen.

Then perhaps for each sensor object you overload the << operator to your needs.

class CFileWrite {
    protected:
    void printToFileAndScreen(string data);
};

class CSensor : public CFileWrite {
   friend ostream& operator<<(ostream &s, CSensor &obj);

};



Your overloaded << can print out any necessary information from the class. Additionally, you could apply this to CFileWrite so that each individual sensor class doesn't have to overload the << if they don't want to.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You