C++ 封装文件操作类

暂时封装一个文件输出类,平时要用的时候方便一些


#include <stdio.h>
#include <string>
using namespace std;

class fileopr
{
	string m_filepath;
	FILE *m_pFile;


public:


	fileopr()
	{
		m_filepath = "d:\\test.txt";
		//m_pFile = fopen(m_filepath.c_str(), "ab+");
		fopen_s(&m_pFile, m_filepath.c_str(), "ab+");
	}


	fileopr(string filepath)
	{
		m_filepath = filepath;
		//m_pFile = fopen(m_filepath.c_str(), "ab+");
		fopen_s(&m_pFile, m_filepath.c_str(), "ab+");
	}


	~fileopr()
	{
		fclose(m_pFile);
	}


	void changefilepath(const string &filepath)
	{
		m_filepath = filepath;
		//重连   
		fclose(m_pFile);
		//m_pFile = fopen(m_filepath.c_str(), "ab+");
		fopen_s(&m_pFile, m_filepath.c_str(), "ab+");
	}


	void outputInt(int Value)
	{
		fprintf(m_pFile, "%d", Value);
	}


	void outputChar(char Value)
	{
		fprintf(m_pFile, "%c", Value);
	}


	void outputString(string strOutput)
	{
		fprintf(m_pFile, "%s", strOutput.c_str());
	}
};

猜你喜欢

转载自blog.csdn.net/Vis_Stu/article/details/77923735