Using a macro for fstream file input as part of a class
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)
{
}
};
加载中,请稍侯......
精彩评论