Thread: Dealing with i/o streams in a class

  1. #1
    Registered User
    Join Date
    Nov 2001

    Dealing with i/o streams in a class

    I'm writing a parser and I have a class tokenize which creates tokens given an input stream. In class tokenize I want to have an istream and ostream that reference the file passed to the constructor, so that I don't need to pass these files on each call to the tokenizer object. I want to be able to open the files independently of the tokenizer object, so I can't just open the files in the constructor. I've thought maybe if I make i/o stream pointers in the class declaration, then do *in = inFile, *out = outFile (inFile and outFile being the files passed by reference to the contructor and in and out being the i/o stream pointers.) The only problem is that in >> <some variable> and out << <whatever> won't really work, maybe *in >> <some variable> and *out << <whatever> will....I guess I don't thoroughly know how streams work. Anyways, any help would be appreciated.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    iostream objects are the same as all other objects/primitives in this respect. If you have a pointer you have to de-reference it before accessing it; so your second guess should work.

  3. #3
    Registered User
    Join Date
    Mar 2002
    You don't pass files around, you pass streams associated with files. I would open a dedicated stream handling file like ifstream to read file (and ofstream to write to file if needed) and associate it with the file of your choice. Then I would pass the stream back and forth between functions. As long as you don't close the stream someplace it will still be associated with the original file. (You can close the stream and associate it with another at any point you wish.)

    The streams you are passing around are actually stream objects, or instances of stream classes. There are a number of methods and operators associated with each stream class. These methods all take some sort of parameter and do something with it. Unfortunately, the object doesn't know what to do with it the parameter until you tell it what to do. Therefore, the >> and << operators need to be overloaded for each of the classes you write if you wish to use the operator on the object as a whole as opposed to a member variable embedded within the object. If you overload the >> operator for the istream class, it should be available for the ifstream class as well, although don't quote me on that.

    Here's an example:

    #include <fstream.h>

    class Sample
    int data;
    friend operator << (ostream & Cout, Sample & s);

    friend operator << (ostream & Cout, Sample & s)
    Cout <<;

    int main()
    Sample sample; = 3;
    cout << sample << endl;
    cout << << endl;

    ofstream fout("myFile.txt");
    fout << << endl;
    fout << sample;

    return 0;

  4. #4
    Registered User
    Join Date
    Nov 2001
    thanks sorensen, I think I'll actually go try it now. Just wanted a general idea of what would work before I went and started working on the class. elad, thanks but that doesn't help at all. I know how to overload an operator, that's not what I wanted to the question more carefully next time ;x have a good day all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Cant comprehend C++ I/O and streams.
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2003, 07:21 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM