0
点赞
收藏
分享

微信扫一扫

c++ builder 比较文本文件内容是否相同

扒皮狼 2022-02-10 阅读 118

//有需要比较两个文本文件内容是否相同的情况,比如参数等,方法:逐行比较

 #include <vcl.h>
#include <fstream>
#include <iostream>
using namespace std;
bool FileCompare(String file1,String file2)//比较函数
{
    char buffer[2000];char buffer2[2000];//每行都读到字符串数组,
    int LineMaxlen=1900;
    int i=0;
    bool IsSame[5000]={false};//是否相同的数组,最多5000行
    bool SameTemp=true;//最终比较结果
    ifstream test (file1.c_str());
    ifstream test2 (file2.c_str());

    if (test.is_open() && test2.is_open())//条件:两个文件都打开
    {
        while (!test.eof() ||!test2.eof() )//退出while循环条件:读到末尾
        {   test.getline (buffer,LineMaxlen);//读第1个文件的一行到buffer

            test2.getline (buffer2,LineMaxlen);//读第2个文件的一行到buffer2
            //cout <<i<<"1,"<<"len="<<strlen(buffer)<<","<< buffer << endl;
            //cout <<i<<"2,"<<"len="<<strlen(buffer2)<<","<< buffer2 << endl;
            IsSame[i]=(*buffer==*buffer2);//比较两个文件的每一行,比较结果放到数组
            //cout<<i<<","<<IsSame[i]<<endl;
            SameTemp=SameTemp&&IsSame[i];//

              i++;
            //if (i>5000) break;
        }
        test.close();
        test2.close();
        //cout<<SameTemp;
    }
    return SameTemp;
}


int main(int argc, char* argv[])//c++  builder 6.0

//int _tmain(int argc, _TCHAR* argv[])//c++  builder 10.3
{cout<<FileCompare("d:\\test111\\test333.txt",
                "d:\\test333.txt");
    system("pause");
    return 0;
}

举报

相关推荐

0 条评论