GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2008, 06:10
Vicki Vicki is offline
New Member
 
Join Date: Feb 2008
Posts: 6
Vicki is on a distinguished road
Question

I/O to a file from within a class


I 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:
 
#include <fstream>

using namespace std;

ofstream fout;

int main(int argc, char *argv[])
{ 
	fout.open("output.txt");		

	if (fout.fail())
	{
		cout << "Output file has failed to open." << endl;
		exit(1);
	} 
             
             fout << "test" << endl;

             fout.close();
	
	system("PAUSE");
	return 0;
}

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:
void Matrix::print()
{
    for(int i=0; i<=(r-1); i++)
    {
        cout << "  [ ";
        for(int j=0; j<=(c-1); j++)
        {
            cout << elements [i][j] << " ";
        }
        cout << " ]" << endl;
    }
    cout << endl;
}


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  
Old 02-Nov-2008, 08:48
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: I/O to a file from within a class


Quote:
Originally Posted by Vicki
At the moment this data is being printed to the screen using cout, and I want to change it to print to a file.
Consider the following example:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void output(ostream&, int[], int);

int
main() {
    int a[] = { 0, 1, 2 };
    ofstream fout;

    fout.open("output.txt");
    
    output(cout, a, sizeof(a) / sizeof(int));
    output(fout, a, sizeof(a) / sizeof(int));

    fout.close();
    
    return 0;
}

void
output(ostream &out, int a[], int n) {
    const int w = 4;

    out << setw(w) << "[";

    for (int i = 0; i < n; ++i)
        out << setw(w) << a[i];

    out << setw(w) << "]" << endl;
}
Quote:
I've tried reading up on this, but any books ive found dont seem to go into this detail.
cplusplus.com is a good online resource which covers this issue & many others:

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  
Old 04-Nov-2008, 05:37
Vicki Vicki is offline
New Member
 
Join Date: Feb 2008
Posts: 6
Vicki is on a distinguished road

Re: I/O to a file from within a class


Thanks 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  
Old 04-Nov-2008, 06:40
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: I/O to a file from within a class


Quote:
Originally Posted by Vicki
Thanks 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.

Yes, that is how it should be done. Please post your error messages so that we can try to help you.
  #5  
Old 04-Nov-2008, 07:35
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: I/O to a file from within a class


Quote:
Originally Posted by Vicki
Iv tried playing around with this but cant get it to work.
The header file will have to be #include'd in every .cpp file that uses the member functions declared. At the same time, all .cpp files will have to all be specified to the compiler/linker or else create a project file (if Microsoft...) or Makefile (if anything else...).
Last edited by ocicat : 04-Nov-2008 at 08:26.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 10:27.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.