#include #include #include #include #include
using namespace std; const size_t file_lenght = 260; const size_t str_lenght = 256; const char* forbidden_chars = "*"<>?|";
bool isValidFilename(const char* filename) { for (size_t i = 0; i < strlen(forbidden_chars); ++i) { if (strchr(filename, forbidden_chars[i]) != nullptr) { return false; } }
const char* colon = strchr(filename, ':'); if (colon != nullptr) { if (colon - filename != 1) { return false; } if (!isalpha(filename[0])) { return false; } if (*(colon + 1) != '\\') { return false; } } const char* dot = strrchr(filename, '.'); if (dot != nullptr) { if (strlen(dot) != 4) { return false; } char ext[5]; strncpy(ext, dot, 4); ext[4] = '\0'; for (int i = 0; i < 4; ++i) { ext[i] = tolower(ext[i]); } if (strcmp(ext, ".txt") != 0) { return false; } } return true;
}
void addTxtExtension(char* filename) { if (strrchr(filename, '.') == nullptr) { strcat(filename, ".txt"); } }
int main() { char filename[file_lenght]; cout << "Имя файла: "; cin.getline(filename, file_lenght);
if (!isValidFilename(filename)) { return 1; } addTxtExtension(filename); if (!isValidFilename(filename)) { return 1; } FILE* file = fopen(filename, "rb"); if (file == nullptr) { return 1; } if (fseek(file, 0, SEEK_END) != 0) { return 1; } long file_size = ftell(file); if (file_size == -1L) { return 1; } rewind(file); char* file_content = (char*)malloc(file_size + 1); if (file_content == nullptr) { return 1; } size_t read_size = fread(file_content, sizeof(char), file_size, file); if (read_size != (size_t)file_size) { free(file_content); fclose(file); return 1; } file_content[file_size] = '\0'; fclose(file); char search_string[str_lenght]; cout << "Поиск строки: "; cin.getline(search_string, str_lenght); int count = 0; char* pos = file_content; size_t search_len = strlen(search_string); if (search_len == 0) { free(file_content); return 1; } while ((pos = strstr(pos, search_string)) != nullptr) { count++; pos += search_len; } cout << "Число вхождений \"" << search_string << "\": " << count << endl; free(file_content); return 0;
Срок выполнения не установлен.
Зависимостей нет.
Удаление ветки НЕОБРАТИМО. Действие никак нельзя отменить.
#include
#include
#include
#include
#include
using namespace std;
const size_t file_lenght = 260;
const size_t str_lenght = 256;
const char* forbidden_chars = "*"<>?|";
bool isValidFilename(const char* filename) {
for (size_t i = 0; i < strlen(forbidden_chars); ++i) {
if (strchr(filename, forbidden_chars[i]) != nullptr) {
return false;
}
}
}
void addTxtExtension(char* filename) {
if (strrchr(filename, '.') == nullptr) {
strcat(filename, ".txt");
}
}
int main() {
char filename[file_lenght];
cout << "Имя файла: ";
cin.getline(filename, file_lenght);
}