linux下的代码对比程序设计
来源:互联网 发布:java web漏洞扫描工具 编辑:程序博客网 时间:2024/05/17 18:47
操作系统为linux,可以移植到vc中,采用的是linux下的vim编辑器。vc下尝试也可以运行
主要是考虑到在学习编程期间需要有很多的例子需要模仿网上或者书上的东西,然后,vim下没有提示功能,所以只能手动输入,这就有可能造成代码的输入错误,然后吧,就是悲剧,如果代码少的话还好,可以根据提示找错,顺便锻炼自己的纠错能力,但是错误满屏或者跟多就悲剧了吧,所以,就自己动手做了个文件对比程序。主要用来比较代码,因为是控制台的,所以地址的输入可能就有点问题。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <sstream>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef vector<string> Vec; //定义vector容器的数据类型
typedef Vec::iterator Iter; //定义vector容器迭代器类型
Vec Vcom; //读取文件内容并存储的vec
Vec Vres; //全局的存储文件不同的内容vec
bool do_open(string path);
bool i_compare(Vec a,Vec b);
bool createFlie();
int main()
{
string ready="";
cout<<"It can compare two file,are you ready?(y/Y or n/N): ";
cin>>ready;
if(ready!="y"||ready!="Y")
{
Vec test1,test2;
string path1,path2;
bool is_ok=false;
cout<<"Input the first file path: ";
while(!is_ok) //循环输入地址
{
cin>>path1;
if(do_open(path1))
{
is_ok=true;
test1=Vcom;
}
else
{
cout<<"the first file con't open"<<endl;
cout<<"Input first path again: ";
is_ok=false;
}
}
is_ok=false;
cout<<"Input the second file path: ";
while(!is_ok)
{
cin>>path2;
if(do_open(path2))
{
is_ok=true;
test2=Vcom;
}
else
{
cout<<"the second file con't open"<<endl;
cout<<"Input second path again: ";
is_ok=false;
}
}
if(i_compare(test1,test2))
{
cout<<"first file equal second file!"<<endl;
}
else
{
cout<<"first file con't equal second file!"<<endl;
if(createFlie())
cout<<"compare create ok !"<<endl;
else
cout<<"compare create error !"<<endl;
//-----------------------------------//
}
}
else
{
cout<<"Thank you to use"<<endl;
}
return 0;
}
//打开文件并按行存储到vector中
bool do_open(string path)
{
Vcom.clear();
ifstream fin(path.c_str()); //文件流操作
if(!fin.is_open())
return false;
Iter it;
string buffer;
string::iterator sit;
while(!fin.eof())
{
getline(fin,buffer,'\n');
sit=buffer.begin();
while(sit!=buffer.end())
{
if(*sit==' ')
sit=buffer.erase(sit);
else
++sit;
}
Vcom.push_back(buffer);
}
if(Vcom.empty())
return false;
fin.close();
return true;
}
//比较文件内容,主要就是比较两个vector中的行
bool i_compare(Vec a,Vec b)
{
string buffer;
int len=abs((int)a.size()-(int)b.size()); //求绝对值,这里必须要强制类型转换为int型
//-----------填充vector,使size相同
if(a.size()>b.size())
{
int i;
for(i=0;i<len;i++)
b.push_back("");
}
else
{
int i;
for(i=0;i<len;i++)
a.push_back("");
}
//------------
int con=1;
Iter it_a=a.begin();
Iter it_b=b.begin();
while(it_a!=a.end())
{
//-------------获取不同的行内容和行号 对比着存储在vres中
if(((*it_a).compare(*it_b)))
{
stringstream strstream;
strstream<<"--"<<con<<" line: ";
buffer="a:"+strstream.str()+*it_a;
Vres.push_back(buffer);
buffer="b:"+strstream.str()+*it_b+"\n";
Vres.push_back(buffer);
}
//-------------
++it_a;
++it_b;
++con;
}
if(!Vres.empty())
return false;
else
return true;
}
//创建结果文件
bool createFlie()
{
string path;
cout<<"Default path?(y/n):";
cin>>path;
if(path=="y")
path="/home/tgomc/linhai/debug";
else
{
cout<<"input the new path:";
cin>>path;
}
path=path+"/com.txt";
ofstream file(path.c_str());//覆盖创建
Iter it;
for(it=Vres.begin();it!=Vres.end();it++)
{
file<<*it<<endl;
}
file.close();
if(Vres.empty())
return false;
else
return true;
}
- linux下的代码对比程序设计
- linux下的chat程序设计
- Linux下的定时器-程序设计
- Linux下的定时器-程序设计
- 求助,《linux程序设计》 的一段代码
- linux下代码对比工具 diffuse 和 meld
- 推荐一款linux下的对比工具
- Linux下常见文件系统的对比
- linux下PWM程序设计
- Linux下程序设计基础
- Linux下串口程序设计
- linux程序设计入门----进程介绍:linux下进程的创建
- 代码的对比directx9
- 代码的对比
- Linux与Windows下的Java开发对比
- Linux与Windows下的Java开发对比
- linux下程式对比工具meld的安装
- Linux与Windows下的Java开发对比
- 框架流量的应用
- 解决访问谷歌慢
- 设计模式之行为型模式
- Eclipse集成Tomcat Tomcatplugin
- Mac 下配置 Git 服务器
- linux下的代码对比程序设计
- 学习用Three20
- MFC 控件具有XP风格
- PHP写日志
- EventHandler 事件 (Adding)
- USB camera功能添加
- Radio Button控件的简单用法
- 网页制作特效
- 内部矛盾