Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

78 строки
1.5 KiB
C++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
char fileName[260];
cin.getline(fileName, 260);
if (strchr(fileName, '*') || strchr(fileName, '"') || strchr(fileName, '<') ||
strchr(fileName, '>') || strchr(fileName, '?') || strchr(fileName, '|') ||
strchr(fileName + 2, ':') || (fileName[0] == ':')) {
cout << "Invalid path" << endl;
return 1;
}
if (fileName[1] == ':' && (!isalpha(fileName[0]) || fileName[2] != '\\')) {
cout << "Invalid path" << endl;
return 1;
}
if (strrchr(fileName, '.') && (strrchr(fileName, '.') > strrchr(fileName, '\\')) && (strrchr(fileName, '.') > strrchr(fileName, '/'))) {
char* ext = strrchr(fileName, '.') + 1;
if (strlen(ext) != 3) {
cout << "Invalid path" << endl;
return 1;
}
ext[0] = tolower(ext[0]);
ext[1] = tolower(ext[1]);
ext[2] = tolower(ext[2]);
if (strncmp(ext, "txt", 3) != 0) {
cout << "Invalid path" << endl;
return 1;
}
}
else {
strcat_s(fileName, 260, ".txt");
}
ifstream fin;
fin.open(fileName, ios_base::binary);
if (!fin.is_open()) {
cout << "Can't open file" << endl;
return 1;
}
fin.seekg(0, ios_base::_Seekend);
size_t size = fin.tellg();
fin.seekg(0, ios_base::_Seekbeg);
char* buf = new char[size];
fin.read(buf, size);
fin.close();
char str[64];
cin.getline(str, 64);
char* found = buf - 1;
uint16_t cnt = -1;
do {
++cnt;
++found;
found = strstr(found, str);
} while (found);
delete[] buf;
cout << cnt << " occurrences found" << endl;
cin.get();
return 0;
}