开发者

Using a macro for fstream file input as part of a class

开发者 https://www.devze.com 2023-01-01 01:26 出处:网络
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 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 th开发者_C百科e filename in as an argument to the class constructor, and then the class someClass will access it with fstream.


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, ...);
}


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) 
    {
    }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消