fileoperator.cpp

来自「功能强大的网络蜘蛛软件」· C++ 代码 · 共 121 行

CPP
121
字号
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include "FileOperator.h"

FileOperator::FileOperator()
{

}

FileOperator::~FileOperator()
{

}

void FileOperator::LoadFileToStr(string &s, string filename)
{
	ifstream is(filename.c_str());
	s.erase();
	if(is.bad()) return;
	s.reserve(is.rdbuf()->in_avail());
	char c;
	while(is.get(c))
	{
		if(s.capacity() == s.size())
			s.reserve(s.capacity() * 3);
		s.append(1, c);
	}
	is.close();
}


void FileOperator::WriteToFile(string filename, string content, int mode)
{    string stime="";
	ofstream outfile;
	if(mode==0)
		outfile.open(filename.c_str());
	else
		outfile.open(filename.c_str(), ios::app);    Functions::get_time(stime);
	outfile << stime << "   " << content ;
	outfile.close();
}

void FileOperator::WriteToFile(string filename, int content, int mode)
{    string stime="";
	ofstream outfile;
	if(mode==0)
		outfile.open(filename.c_str());
	else
		outfile.open(filename.c_str(), ios::app);    Functions::get_time(stime);
	outfile << stime << "   " << content ;
	outfile.close();
}

void FileOperator::WriteToFile(string filename, const char *content, int mode)
{    string stime="";
	ofstream outfile;
	if(mode==0)
		outfile.open(filename.c_str());
	else
		outfile.open(filename.c_str(), ios::app);    Functions::get_time(stime);
	outfile << stime << "   " << content ;
	outfile.close();
}

void FileOperator::WriteToFileLn(string filename, string content, int mode)
{    string stime="";
	ofstream outfile;
	if(mode==0)
		outfile.open(filename.c_str());
	else
		outfile.open(filename.c_str(), ios::app);    Functions::get_time(stime);
	outfile << stime << "   " << content << endl;
	outfile.close();
}

void FileOperator::WriteToFileLn(string filename, int content, int mode)
{    string stime="";
	ofstream outfile;
	if(mode==0)
		outfile.open(filename.c_str());
	else
		outfile.open(filename.c_str(), ios::app);    Functions::get_time(stime);
	outfile << stime << "   " << content << endl;
	outfile.close();
}

void FileOperator::WriteToFileLn(string filename, const char *content, int mode)
{    string stime="";
	ofstream outfile;
	if(mode==0)
		outfile.open(filename.c_str());
	else
		outfile.open(filename.c_str(), ios::app);    Functions::get_time(stime);
	outfile << stime << "   " << content << endl;
	outfile.close();
}

void FileOperator::WriteLnToFile(string filename, int num=1)
{
	ofstream outfile;
	outfile.open(filename.c_str(), ios::app);
	while (num-- > 0){
		outfile << endl;
	}
	outfile.close();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?