Родитель
7745a023df
Сommit
11737611b6
@ -1,127 +1,125 @@
|
|||||||
#include
|
#include <iostream>
|
||||||
#include
|
#include <cstdio>
|
||||||
#include
|
#include <cstring>
|
||||||
#include
|
#include <cctype>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const size_t MAX_FILENAME_LENGTH = 260;
|
const size_t MAX_FILENAME_LENGTH = 260; // Максимальная длина имени файла
|
||||||
|
const size_t MAX_SEARCH_STRING_LENGTH = 256; // Максимальная длина строки для поиска
|
||||||
|
const char* forbidden_chars = "*<>\?|"; // Запрещенные символы в имени файла
|
||||||
|
|
||||||
const size_t MAX_SEARCH_STRING_LENGTH = 256;
|
|
||||||
|
|
||||||
const char* forbidden_chars = "*"<>?|";
|
|
||||||
|
|
||||||
bool isValidFilename(const char* filename) {
|
bool isValidFilename(const char* filename) {
|
||||||
for (size_t i = 0; i < strlen(forbidden_chars); ++i) {
|
if (filename == nullptr || strlen(filename) == 0) {
|
||||||
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;
|
return false;
|
||||||
}
|
}
|
||||||
if (*(colon + 1) != '\\') {
|
for (size_t i = 0; i < strlen(forbidden_chars); ++i) {
|
||||||
return false;
|
if (strchr(filename, forbidden_chars[i]) != nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const char* dot = strrchr(filename, '.');
|
const char* colon = strchr(filename, ':');
|
||||||
if (dot != nullptr) {
|
if (colon != nullptr) {
|
||||||
if (strlen(dot) != 4) {
|
if (colon - filename != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
char ext[5];
|
if (!isalpha(filename[0])) {
|
||||||
strncpy(ext, dot, 4);
|
return false;
|
||||||
ext[4] = '\0';
|
if (*(colon + 1) != '\\' )
|
||||||
for (int i = 0; i < 4; ++i) {
|
{
|
||||||
ext[i] = tolower(ext[i]);
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (strcmp(ext, ".txt") != 0) {
|
|
||||||
return false;
|
// Проверка наличия .txt
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
// Функция для добавления расширения .txt к имени файла, если его нет
|
||||||
void addTxtExtension(char* filename) {
|
void addTxtExtension(char* filename) {
|
||||||
if (strrchr(filename, '.') == nullptr) {
|
if (strrchr(filename, '.') == nullptr) {
|
||||||
strcat(filename, ".txt");
|
strcat(filename, ".txt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
char filename[MAX_FILENAME_LENGTH];
|
||||||
|
cout << "Введите имя файла: ";
|
||||||
|
cin.getline(filename, MAX_FILENAME_LENGTH);
|
||||||
|
|
||||||
char filename[MAX_FILENAME_LENGTH];
|
addTxtExtension(filename);
|
||||||
cout << "Введите имя файла: ";
|
|
||||||
cin.getline(filename, MAX_FILENAME_LENGTH);
|
|
||||||
if (!isValidFilename(filename)) {
|
|
||||||
cout << "Некорректное имя файла." << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (!isValidFilename(filename)) {
|
|
||||||
cout << "Некорректное имя файла после добавления расширения." << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE* file = fopen(filename, "rb");
|
if (!isValidFilename(filename)) {
|
||||||
if (file == nullptr) {
|
cout << "Некорректное имя файла." << endl;
|
||||||
cout << "Не удалось открыть файл: " << filename << endl;
|
return 1;
|
||||||
return 1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
long file_size = ftell(file);
|
FILE* file = fopen(filename, "rb");
|
||||||
if (file_size == -1L) {
|
if (file == nullptr) {
|
||||||
cout << "Не удалось определить размер файла." << endl;
|
cout << "Не удалось открыть файл: " << filename << endl;
|
||||||
fclose(file);
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
rewind(file);
|
|
||||||
char* file_content = (char*)malloc(file_size + 1);
|
|
||||||
if (file_content == nullptr) {
|
|
||||||
cout << "Не удалось выделить память для содержимого файла." << endl;
|
|
||||||
fclose(file);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t read_size = fread(file_content, sizeof(char), file_size, file);
|
fseek(file, 0, SEEK_END);
|
||||||
if (read_size != (size_t)file_size) {
|
long file_size = ftell(file);
|
||||||
cout << "Ошибка при чтении файла." << endl;
|
|
||||||
free(file_content);
|
|
||||||
fclose(file);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
file_content[file_size] = '\0';
|
char* file_content = (char*)malloc(file_size + 1);
|
||||||
|
if (file_content == nullptr) {
|
||||||
|
cout << "Не удалось выделить память для содержимого файла." << endl;
|
||||||
|
fclose(file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
fclose(file);
|
size_t read_size = fread(file_content, sizeof(char), file_size, file);
|
||||||
|
if (read_size != (size_t)file_size) {
|
||||||
|
cout << "Ошибка при чтении файла." << endl;
|
||||||
|
free(file_content);
|
||||||
|
fclose(file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
file_content[file_size] = '\0';
|
||||||
|
|
||||||
char search_string[MAX_SEARCH_STRING_LENGTH];
|
fclose(file);
|
||||||
cout << "Введите строку для поиска: ";
|
|
||||||
cin.getline(search_string, MAX_SEARCH_STRING_LENGTH);
|
|
||||||
|
|
||||||
int count = 0;
|
char search_string[MAX_SEARCH_STRING_LENGTH];
|
||||||
char* pos = file_content;
|
cout << "Введите строку для поиска: ";
|
||||||
|
cin.getline(search_string, MAX_SEARCH_STRING_LENGTH);
|
||||||
|
|
||||||
size_t search_len = strlen(search_string);
|
int count = 0;
|
||||||
if (search_len == 0) {
|
char* pos = file_content;
|
||||||
cout << "Пустая строка для поиска." << endl;
|
|
||||||
free(file_content);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((pos = strstr(pos, search_string)) != nullptr) {
|
size_t search_len = strlen(search_string);
|
||||||
count++;
|
if (search_len == 0) {
|
||||||
pos += search_len;
|
cout << "Пустая строка для поиска." << endl;
|
||||||
}
|
free(file_content);
|
||||||
cout << "Число вхождений строки \"" << search_string << "\": " << count << endl;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((pos = strstr(pos, search_string)) != nullptr) {
|
||||||
|
count++;
|
||||||
|
pos += search_len;
|
||||||
|
}
|
||||||
|
cout << "Число вхождений строки \"" << search_string << "\": " << count << endl;
|
||||||
|
|
||||||
free(file_content);
|
free(file_content);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Загрузка…
Ссылка в новой задаче