C++ Program to Detect Line Repetition in a File
Here is a C++ program that reads a file and checks for line repetition:
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <string>
int main() {
std::ifstream file('input.txt'); // Replace 'input.txt' with the actual file name
if (!file) {
std::cerr << "Failed to open the file." << std::endl;
return 1;
}
std::unordered_set<std::string> lines;
std::string line;
bool repeatedLine = false;
while (std::getline(file, line)) {
if (lines.find(line) != lines.end()) {
repeatedLine = true;
break;
}
lines.insert(line);
}
if (repeatedLine) {
std::cout << "There is a repeated line in this file." << std::endl;
} else {
std::cout << "No repeated lines in this file." << std::endl;
}
file.close();
return 0;
}
Make sure to replace 'input.txt' with the actual file name you want to check for line repetition. The program uses an unordered set to store unique lines read from the file. If a line is found to be already present in the set, it means there is a repeated line in the file. Otherwise, the program concludes that there are no repeated lines.
原文地址: https://www.cveoy.top/t/topic/o1Sp 著作权归作者所有。请勿转载和采集!