compare.cpp

来自「浙江大学的悟空嵌入式系统模拟器」· C++ 代码 · 共 110 行

CPP
110
字号
#include "Compare.h"

#include <assert.h>
#include <iomanip>

#define LINE_CHAR 256

namespace ARM{

	Compare & Compare::instance()
	{
		static Compare comp;
		return comp;
	}

	Compare::~Compare()
	{
		if(out != NULL)
		{
			out->close();
			delete out;
		}
	}

	void Compare::clear(std::string file)
	{
		std::ofstream fs(file.c_str(), std::ios::out);
		fs.close();
	}

	bool Compare::open_file(std::string file)
	{
		out = new std::ofstream(file.c_str(), std::ios::app);
		if(out == NULL)
		{
			std::cerr << "open file: " << file << " error.";
			return false;
		}
		(*out) << std::endl;
		return true;
	}

	bool Compare::write_to_file(std::string content)
	{

		//std::ofstream out(file.c_str(), std::ios::trunc); 
		//assert( !out );
		(*out) << std::setw(8) << content << std::endl;
		return true;
	}

	bool Compare::append_to_file(std::string content)
	{
		//std::ofstream out(file.c_str(), std::ios::app); 
		//assert( !out );
		(*out) << std::setw(8) << content << std::endl;
		return true;
	}

	bool Compare::append_to_file(unsigned int val)
	{
       (*out) << std::setw(12) << val ;
		return true;
	}

	bool Compare::append_enter()
	{
		(*out) << std::endl;
		return true;
	}

	bool Compare::compare(std::string f1, std::string f2, int line_sum)
	{
		std::ifstream src(f1.c_str());
		std::ifstream des(f2.c_str());

		if(!src || !des)
		{
			std::cerr<< "unload the file" << std::endl;
			src.close();
			des.close();
			return false;
		}
		//unsigned int line_num = 1;
		char c1[LINE_CHAR],c2[LINE_CHAR];

		//f1.get
		//while(f1.eof() || !f2.eof())
		for(int i=1; i <= line_sum; i++)
		{
//			if(f1.eof() != f2.eof())
//			{
//				std::cout << "第 " << i <<" 行不对,有个文件已经结束" << std::endl;
//			}
			src.getline(c1, LINE_CHAR);
			des.getline(c2, LINE_CHAR);
			if(strcmp(c1, c2) !=0)
			{
				std::cout << "第 " << i <<" 行不对" << std::endl;
				return false;
			}
		}
		src.close();
		des.close();
		return true;
	}

	

}

⌨️ 快捷键说明

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