⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compfile.cpp

📁 软件课程设计(1) 是一些非常经典的程序
💻 CPP
字号:
///////首先让用户输入两个文件名及其路径(二文件均为text文件///////
///////而后通过使用类成员函数getline逐行读入这两个指定文件的/////
///////内容并进行比较。若发现有不同,则在屏幕上显示出相异二行////
////的行号及其内容,并暂停下来询问用户是否需要继续比较后继行//////
//////直到用户回答不需要继续进行比较,或者已经比到了二文件的结束时停止处理/////
#include<fstream.h>
#include<iostream.h>
#include<string>
#include<process.h>

int count=0;

int max(int a,int b)
{
   if(a>b) return a;
   else return b;
}

int Compare(char b1[],char b2[])
{
	int boolean;
	for(int i=1;i<99&&(b1[i-1]!='\0')&&(b2[i-1]!='\0');i++)   //为什么不可以?
	{
	  if(b1[i-1]!=b2[i-1])
	  {   
		  cout<<"The first different char is "<<b1[i-1]<<"&"<<b2[i-1];
		 boolean=1;
	      break;
	  }
	  else boolean=0;
  }
 return boolean;
	
}


/////////////以下注释掉的为用命令行提供参数的代码////////////////
int main()   
            //int main(int argc,char *argv[])
{
	 char c;
	 char filename1[40];
	 char filename2[40];
	 cout<<" Please put in two txt filenames(absolute path):\n";
	 cin>>filename1>>filename2;
	 ifstream infile1(filename1,ios::in);
	 //ifstream infile1(argv[1],ios::in);
	if(!infile1)
	{
       cerr<<"open error!\n";
	   exit(1);
	}
	ifstream infile2(filename2,ios::in);
	//ifstream infile2(argv[2],ios::in);
	if(!infile2)
	{
       cerr<<"open error!\n";
	   exit(1);
	}
    char buffer1[99];
	char buffer2[99];
	do{
    infile1.getline(buffer1,99,'\n');
	infile2.getline(buffer2,99,'\n');
    count++;
	if(Compare(buffer1,buffer2))
	cout<<"     The different line is : "<<count<<endl;
	else cout<<"Two lines are the same!\n";
	    cout<<"contiue to compare,please enter c:\n";
	    cin>>c;
	if(c!='c')exit(1);
	}while(c=='c'&&!infile1.eof()&&!infile2.eof());

	infile1.close();
    infile2.close();
    return 0;
}

⌨️ 快捷键说明

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