Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions KRNumber3/KRNumber3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <stdbool.h>

bool scanFile(char fileName[], char arrayForTest[]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool scanFile(char fileName[], char arrayForTest[]) {
bool scanFile(const char fileName[], const char arrayForTest[]) {

Мы их вроде ниже не меняем

FILE* file = fopen(fileName, "r");
if (file == NULL) {
printf("Mistake!");
return false;
}
char data[100] = { '\0' };
int pointerTest = 0;
while (fscanf(file, "%s", data) == 1){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (fscanf(file, "%s", data) == 1){
while (fscanf(file, "%s", data) == 1) {

size_t sizeData = strlen(data);
if (sizeData >= 1) {
if (fileName != NULL && strcmp(fileName, "test.txt") != 0) {
printf("%c", data[0]);
} else {
if (arrayForTest[pointerTest] != data[0]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще, в боевом коде не должно быть ничего про тесты. Тест должен тестировать наблюдаемое извне поведение настоящего кода, а не какой-то кастомный код, написанный специально под тест в реальной системе.

return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Файл останется незакрытым

}
++pointerTest;
}
}
for (size_t i = 1; i < sizeData; ++i) {
if (data[i] != data[i - 1]) {
if (fileName != NULL && strcmp(fileName, "test.txt") != 0) {
printf("%c", data[i]);
} else {
if (arrayForTest[pointerTest] != data[i]) {
return false;
}
++pointerTest;
}
}
}
}
fclose(file);
return true;
}

bool test(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool test(){
bool test() {

char fileName[9] = "test.txt";
char arrayTest[4] = "abc";
return scanFile(fileName, arrayTest);
Comment on lines +45 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот если бы scanFile принимала const-ы, то эти переменные были бы не нужны

}

int main(void) {
setlocale(LC_ALL, "RUS");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что-то тут отступов многовато

if (test()) {
printf("Tests correct\n");
}
else {
printf("Mistake...\n");
return 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 0;
return 1;

}
printf("Enter the file name and its extension, the size should not exceed 100 characters. You cannot enter a file named test.txt.\n");
char fileName[100] = { '\0' };
int checkScan = scanf("%s", &fileName);

while (checkScan != 1 || fileName == NULL || strcmp(fileName, "test.txt") == 0) {
while (getchar() != '\n') {
}
printf("Mistake...Try again\n");
checkScan = scanf("%s", &fileName);
}
if (!scanFile(fileName, NULL)) {
printf("Mistake...\n");
return -1;
}
}
1 change: 1 addition & 0 deletions KRNumber3/KRNumber3/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aabbcc