From e74c692fe63f1f0de3e0aeecfdc99f8df3d3f3a4 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sun, 18 Aug 2024 12:52:44 +0800 Subject: [PATCH] trim space --- src/main.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4cd66d1..7fb12fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -254,26 +254,32 @@ int main(int argc, char* argv[]) { exit(0); } +void trimTrailingWhitespace(std::string& str) { + str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), str.end()); +} + int compareFiles(const std::string& path1, const std::string& path2) { std::ifstream file1(path1); std::ifstream file2(path2); - string s("cmping "); - LOG(s+path1+" "+path2) - if (!file1.is_open()) { std::cerr << "cannot open the file at: " << path1 << std::endl; - exit(1); + exit(-1); } if (!file2.is_open()) { std::cerr << "cannot open the file at: " << path2 << std::endl; - exit(1); + exit(-1); } std::string line1, line2; int lineNumber = 1; while (std::getline(file1, line1) && std::getline(file2, line2)) { + trimTrailingWhitespace(line1); + trimTrailingWhitespace(line2); + if (line1 != line2) { return lineNumber; // 返回第一个不同的行号 } @@ -287,7 +293,6 @@ int compareFiles(const std::string& path1, const std::string& path2) { return 0; // 文件内容完全一致 } - string getProtectPath(const std::filesystem::path &p){ return "\""+p.string()+"\""; }