难以为文件处理重载运算符<< class
Difficulty overloading operator<< for file handling class
我必须:
Define a File_handle
class with constructor that takes a string argument (file name), opens the file in the constructor, and closes it in the destructor.
据我了解,这个 class 用于提供 RAII,我正在尝试使用 FILE*
作为基本数据结构来实现 class,我的目标基本上是FILE*
一个智能指针:
fileHandler.h
:
// Class CFile_handler based on FILE*
class CFile_handler {
public:
CFile_handler(); // default constructor
CFile_handler(const std::string& fileName, // constructor
const std::string& mode);
~CFile_handler (); // destructor
// modifying member function
void open_file(const std::string& fileName,
const std::string& mode);
protected:
typedef FILE* ptr;
private:
CFile_handler(const CFile_handler&); // prevent copy creation
CFile_handler& operator= (const CFile_handler&); // prevent copy assignment
ptr c_style_stream; // data member
};
fileHandler.cpp
:
// Class CFile_handler member implementations
// default constuctor
CFile_handler::CFile_handler() {
}
// constructor
CFile_handler::CFile_handler(const std::string& fileName, const std::string& mode = "r")
: c_style_stream( fopen( fileName.c_str(), mode.c_str() ) )
{
}
// destructor
CFile_handler::~CFile_handler() {
if (c_style_stream) fclose(c_style_stream);
}
// Modifying member functions
void CFile_handler::open_file(const std::string& fileName, const std::string& mode) {
c_style_stream = ( fopen( fileName.c_str(), mode.c_str() ) );
}
但是,我在重载 I/O operators<<
/ >>
时遇到困难,因为我不知道如何实现它们中的任何一个。
如何重载 operator<<
以便 class 与 iostream 对象一起工作?
编辑:
正如@LokiAstari 提出的那样,继承istream
并定义自己的streambuf
会是更好的策略。
有人可以给出处理 FILE*
的 streambuf
的实施示例或指导吗?
我要提供的是:
CFile_handler fh("filename.txt", "r");
std::string file_text;
fh >> file_text;
或:
CFile_handler fh("filename.txt", "w");
fh << "write this to file";
你的operator<<
函数是输出一个CFile_handler
对象到C++输出流,不是输出到一个CFile_handler
对象.
要输出到 CFile_handler
对象,您有两种选择:
作为成员函数
CFile_handler& CFile_handler::operator<<(int value)
{
// Output an integer to the contained file
return *this;
}
或者作为一个非成员函数,它将 CFile_handler
引用作为第一个参数:
CFile_handler& operator<<(CFile_handler& file, int value)
{
// Output an integer to the file contained in `file`
return file;
}
对于上述两种变体,您可以执行例如
CFile_handler my_file(...);
my_file << 1234;
您可以使用 std::streambuf 派生 std::streams 的类型来处理 FILE*
#include <iostream>
#include <stdio.h>
class OutputFilePointerStream: public std::ostream
{
class OutputFilePointerStreamBuf: public std::streambuf
{
FILE* buffer;
public:
OutputFilePointerStreamBuf(std::string const& fileName)
{
buffer = fopen(fileName.c_str(), "w");
}
~OutputFilePointerStreamBuf()
{
fclose(buffer);
}
virtual std::streamsize xsputn(const char* s, std::streamsize n) override
{
static char format[30];
sprintf(format, "%%.%lds", n);
fprintf(buffer, format, s);
return n;
}
};
OutputFilePointerStreamBuf buffer;
public:
OutputFilePointerStream(std::string const& fileName)
: std::ostream(nullptr)
, buffer(fileName)
{
rdbuf(&buffer);
}
};
int main()
{
OutputFilePointerStream fileStream("Test");
fileStream << "Testing: " << 5 << "><\n";
fileStream << "Line Again\n";
}
我必须:
Define a
File_handle
class with constructor that takes a string argument (file name), opens the file in the constructor, and closes it in the destructor.
据我了解,这个 class 用于提供 RAII,我正在尝试使用 FILE*
作为基本数据结构来实现 class,我的目标基本上是FILE*
一个智能指针:
fileHandler.h
:
// Class CFile_handler based on FILE*
class CFile_handler {
public:
CFile_handler(); // default constructor
CFile_handler(const std::string& fileName, // constructor
const std::string& mode);
~CFile_handler (); // destructor
// modifying member function
void open_file(const std::string& fileName,
const std::string& mode);
protected:
typedef FILE* ptr;
private:
CFile_handler(const CFile_handler&); // prevent copy creation
CFile_handler& operator= (const CFile_handler&); // prevent copy assignment
ptr c_style_stream; // data member
};
fileHandler.cpp
:
// Class CFile_handler member implementations
// default constuctor
CFile_handler::CFile_handler() {
}
// constructor
CFile_handler::CFile_handler(const std::string& fileName, const std::string& mode = "r")
: c_style_stream( fopen( fileName.c_str(), mode.c_str() ) )
{
}
// destructor
CFile_handler::~CFile_handler() {
if (c_style_stream) fclose(c_style_stream);
}
// Modifying member functions
void CFile_handler::open_file(const std::string& fileName, const std::string& mode) {
c_style_stream = ( fopen( fileName.c_str(), mode.c_str() ) );
}
但是,我在重载 I/O operators<<
/ >>
时遇到困难,因为我不知道如何实现它们中的任何一个。
如何重载 operator<<
以便 class 与 iostream 对象一起工作?
编辑:
正如@LokiAstari 提出的那样,继承istream
并定义自己的streambuf
会是更好的策略。
有人可以给出处理 FILE*
的 streambuf
的实施示例或指导吗?
我要提供的是:
CFile_handler fh("filename.txt", "r");
std::string file_text;
fh >> file_text;
或:
CFile_handler fh("filename.txt", "w");
fh << "write this to file";
你的operator<<
函数是输出一个CFile_handler
对象到C++输出流,不是输出到一个CFile_handler
对象.
要输出到 CFile_handler
对象,您有两种选择:
作为成员函数
CFile_handler& CFile_handler::operator<<(int value) { // Output an integer to the contained file return *this; }
或者作为一个非成员函数,它将
CFile_handler
引用作为第一个参数:CFile_handler& operator<<(CFile_handler& file, int value) { // Output an integer to the file contained in `file` return file; }
对于上述两种变体,您可以执行例如
CFile_handler my_file(...);
my_file << 1234;
您可以使用 std::streambuf 派生 std::streams 的类型来处理 FILE*
#include <iostream>
#include <stdio.h>
class OutputFilePointerStream: public std::ostream
{
class OutputFilePointerStreamBuf: public std::streambuf
{
FILE* buffer;
public:
OutputFilePointerStreamBuf(std::string const& fileName)
{
buffer = fopen(fileName.c_str(), "w");
}
~OutputFilePointerStreamBuf()
{
fclose(buffer);
}
virtual std::streamsize xsputn(const char* s, std::streamsize n) override
{
static char format[30];
sprintf(format, "%%.%lds", n);
fprintf(buffer, format, s);
return n;
}
};
OutputFilePointerStreamBuf buffer;
public:
OutputFilePointerStream(std::string const& fileName)
: std::ostream(nullptr)
, buffer(fileName)
{
rdbuf(&buffer);
}
};
int main()
{
OutputFilePointerStream fileStream("Test");
fileStream << "Testing: " << 5 << "><\n";
fileStream << "Line Again\n";
}