diff --git a/sk1/main.c b/sk1/main.c index 2e3cbea..fdbdb09 100644 --- a/sk1/main.c +++ b/sk1/main.c @@ -26,8 +26,11 @@ int main() { // Запитуємо користувача про вибір printf("Enter your choice (0 to exit): "); if (scanf("%d", &choice) != 1) { - fprintf(stderr, "Invalid input. Exiting.\n"); - break; + // Якщо введення не є числом, виводимо повідомлення про помилку + fprintf(stderr, "Invalid input. Please enter a valid number (0-4).\n"); + // Очищаємо буфер введення від непотрібних символів + while(getchar() != '\n'); + continue; } if (choice == 0) { @@ -35,12 +38,34 @@ int main() { break; } + if (choice < 0 || choice > 4) { + // Перевіряємо, чи вибір знаходиться в допустимому діапазоні + printf("Invalid choice. Please select a valid option (1-4).\n"); + continue; + } + // Вводимо імена файлів printf("Enter input file name: "); scanf("%s", infile); printf("Enter output file name: "); scanf("%s", outfile); + // Перевіряємо, чи існує вхідний файл, і чи доступний вихідний файл для запису + FILE *infile_ptr = fopen(infile, "rb"); + if (!infile_ptr) { + printf("Error: Input file does not exist or cannot be opened.\n"); + continue; + } + fclose(infile_ptr); + + FILE *outfile_ptr = fopen(outfile, "wb"); + if (!outfile_ptr) { + printf("Error: Output file cannot be created or opened.\n"); + continue; + } + fclose(outfile_ptr); + + // Виконання обраного алгоритму switch (choice) { case 1: compress_1(infile, outfile);