tags:

views:

65

answers:

2

I have a class that processes a file, and as part of the constructor with one argument I want to input a file using fstream.

I basically want it do something like this

class someClass{
   public:
      someClass(char * FILENAME)
      {   
          fstream fileToProcess;
          fileToProcess.open(<FILENAME>, fstream::in | fstream::out | fstream::app);
      }
};

I want to pass the filename in as an argument to the class constructor, and then the class someClass will access it with fstream.

+3  A: 

You can do it just the way as you lay it out in the question. Simply pass the string given to the constructor on to the fstream's open() method:

someClass(const char *filename)
{   
    fstream fileToProcess;
    fileToProcess.open(filename, ...);
}
sth
+1 for spotting the missing `const`
MSalters
+2  A: 

You don't need a macro, and you don't have to explicitly call open.

using std::fstream;

class someClass
{
    fstream fileToProcess;
    public:
    someClass(char * filename) 
    : fileToProcess(filename, fstream::in | fstream::out | fstream::app) 
    {
    }
};
Matthew Flaschen