![]() |
|
#1
|
|||
|
|||
I/O to a file from within a classI am writing a computer program as part of my dissertation, but my C++ skills aren't the best. The program is currently set up with a number of classes, each outputting different data. At the moment this data is being printed to the screen using cout, and I want to change it to print to a file.
So far, I have succeeded in using fstream to print data to the file from within main(), but don't know how to go about printing from the classes. Here's a simple version of what I've used within main(); CPP / C++ / C Code:
One of my classes, "Matrix", includes a print function that prints the elements in an mxn Matrix to the screen, using cout. The relevent section of Matrix.cpp is included for clarity; CPP / C++ / C Code:
I'm wondering how I would change this to output to the file "Output.txt" I've created in main()? Or is this even possible? I've tried reading up on this, but any books ive found dont seem to go into this detail. I hope this makes some sense and would really appreciate any help anyone can offer. Thanks. |
|||
|
#2
|
|||
|
|||
Re: I/O to a file from within a classQuote:
CPP / C++ / C Code:
Quote:
http://www.cplusplus.com/reference/i...tor%3C%3C.html Lastly, if your code satisfies the need in which it is written, great. However, there are situations where tightly coupling the purpose of a class with I/O can later lead to other issues. This isn't a plea for you to change your code as it is now, but you may want to recognize that making print() a member function to class Matrix ties I/O to the abstraction of a mathematical construct. |
|
#3
|
|||
|
|||
Re: I/O to a file from within a classThanks a million that's a great help.
One other question, is it possible to save the function declaration as a header file and the function definition as a cpp file? Iv tried playing around with this but cant get it to work. |
|
#4
|
|||
|
|||
Re: I/O to a file from within a classQuote:
Yes, that is how it should be done. Please post your error messages so that we can try to help you. |
|
#5
|
|||
|
|||
Re: I/O to a file from within a classQuote:
Last edited by ocicat : 04-Nov-2008 at 08:26.
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 20:23 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 21:03 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
| CD Buring Failed | skanth2000 | Computer Hardware Forum | 1 | 15-Nov-2003 04:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The