From 736c1feea9bf327a6cf6c1ef57fde4ba308dffbc Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 11 Dec 2022 18:21:09 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=9A=D0=9C=D0=9F=20=D0=B8=20=D0=B2=D0=B2=D0=BE=D0=B4=20=D1=87?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B7=20=D1=84=D0=B0=D0=B9=D0=BB,=20=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BB=D0=B8=D1=81=D1=8C=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- substringSearch/substringSearch.sln | 31 ++++ substringSearch/substringSearch/main.c | 172 ++++++++++++++++++ .../substringSearch/substringSearch.vcxproj | 150 +++++++++++++++ .../substringSearch.vcxproj.filters | 27 +++ substringSearch/substringSearch/test.txt | 1 + 5 files changed, 381 insertions(+) create mode 100644 substringSearch/substringSearch.sln create mode 100644 substringSearch/substringSearch/main.c create mode 100644 substringSearch/substringSearch/substringSearch.vcxproj create mode 100644 substringSearch/substringSearch/substringSearch.vcxproj.filters create mode 100644 substringSearch/substringSearch/test.txt diff --git a/substringSearch/substringSearch.sln b/substringSearch/substringSearch.sln new file mode 100644 index 0000000..4fde923 --- /dev/null +++ b/substringSearch/substringSearch.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "substringSearch", "substringSearch\substringSearch.vcxproj", "{991473C0-010E-492B-91D1-3EFBDEB6FAB3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x64.ActiveCfg = Debug|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x64.Build.0 = Debug|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x86.ActiveCfg = Debug|Win32 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x86.Build.0 = Debug|Win32 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x64.ActiveCfg = Release|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x64.Build.0 = Release|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x86.ActiveCfg = Release|Win32 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7A1CB94E-03B1-4922-AC28-FD130F1083F6} + EndGlobalSection +EndGlobal diff --git a/substringSearch/substringSearch/main.c b/substringSearch/substringSearch/main.c new file mode 100644 index 0000000..0eef95a --- /dev/null +++ b/substringSearch/substringSearch/main.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include + +char* getLine(int* lengthString, int* errorCode) { + *lengthString = 0; + int index = 1; + char* string = (char*)malloc(sizeof(char)); + if (string == NULL) { + *errorCode = -1; + return NULL; + } + char symbol = getchar(); + while (symbol != '\n') { + string[*lengthString] = symbol; + ++(*lengthString); + if (*lengthString >= index) { + index *= 2; + char* copy = (char*)realloc(string, index * sizeof(char)); + if (copy == NULL) { + *errorCode = -1; + free(string); + return NULL; + } + string = copy; + } + symbol = getchar(); + } + if (string == NULL) { + *errorCode = -1; + return NULL; + } + string[*lengthString] = '\0'; + + return string; +} + +int* prefixFunction(const char* string) { + int* prefixArray = calloc(strlen(string), sizeof(int)); + if (prefixArray == NULL) { + return NULL; + } + prefixArray[0] = 0; + size_t sizeString = strlen(string); + for (size_t i = 1, j = 0; i < sizeString; ++i) { + while (j > 0 && string[j] != string[i]) { + j = prefixArray[j - 1]; + } + if (string[i] == string[j]) { + ++j; + } + prefixArray[i] = j; + } + return prefixArray; +} + +int findIndexSubstring(const char* string, const char* substring, int* errorCode) { + size_t sizeString = strlen(string); + size_t sizeSubstring = strlen(substring); + int result = -1; + if (sizeString == 0 || sizeSubstring == 0 || sizeString < sizeSubstring) { + return -1; + } + int* prefixArray = prefixFunction(substring); + for (int i = 0, j = 0; i < sizeString; ++i) { + while (j > 0 && prefixArray[j] != prefixArray[i]) { + j = prefixArray[j - 1]; + } + if (prefixArray[j] == prefixArray[i]) { + ++j; + } + if (j == sizeSubstring) { + free(prefixArray); + return i - j + 1; + } + } + free(prefixArray); + return -1; +} + +int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const char* substring) { + FILE* file; + *errorCode = fopen_s(&file, nameFile, "r"); + if (*errorCode != 0 || file == NULL) { + *errorCode = -1; + return; + } + + char buffer[400] = { '\0' }; + char copyText[200] = { '\0' }; + int steps = 1; + size_t sizeBuffer = 0; + size_t sizeCopyText = 0; + while (fgets(copyText, 200, file) != NULL) { + switch (steps) + { + case 1: + strcpy(buffer, copyText); + steps = 2; + break; + case 2: + sizeBuffer = strlen(buffer); + sizeCopyText = strlen(copyText); + for (int i = sizeBuffer; i < sizeCopyText; ++i) { + buffer[i] = copyText[i]; + } + steps = 3; + break; + case 3: + sizeBuffer = strlen(buffer); + if (sizeBuffer >= 200) { + int difference = sizeBuffer - 199; + for (int i = sizeBuffer - 199; i < sizeBuffer; ++i) { + buffer[i - difference] = buffer[i]; + } + } + sizeCopyText = strlen(copyText); + for (int i = 0; i < copyText; ++i) { + buffer[i + 200] = copyText[i]; + } + break; + } + int index = findIndexSubstring(buffer, substring, errorCode); + if (index != -1) { + return index; + } + if (*errorCode != 0) { + return -1; + } + memset(copyText, 0, 200); + } +} + +int main() { + printf("Input file name with extension\n"); + int errorCode = 0; + int sizeFile = 0; + const char* nameFile = getLine(&sizeFile, &errorCode); + if (errorCode != 0) { + printf("Error\n"); + return -1; + } + printf("Input substring. No more than 99 symbols\n"); + int sizeSubstring = 0; + char* substring = getLine(&sizeSubstring, &errorCode); + if (errorCode != 0 || substring == NULL) { + printf("Error\n"); + return -1; + } + while (sizeSubstring > 100) { + printf("No more than 99 symbols"); + free(substring); + substring = getLine(&sizeSubstring, &errorCode); + if (errorCode != 0 || substring == NULL) { + printf("Error\n"); + return -1; + } + } + int number = workWithFile(nameFile, &errorCode, sizeSubstring, substring); + if (errorCode != 0) { + printf("Error\n"); + return -1; + } + free(nameFile); + free(substring); + if (number != -1) { + printf("%d\n", number); + } else { + printf("No occurrences found\n"); + } +} \ No newline at end of file diff --git a/substringSearch/substringSearch/substringSearch.vcxproj b/substringSearch/substringSearch/substringSearch.vcxproj new file mode 100644 index 0000000..9f10ac3 --- /dev/null +++ b/substringSearch/substringSearch/substringSearch.vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {991473c0-010e-492b-91d1-3efbdeb6fab3} + substringSearch + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/substringSearch/substringSearch/substringSearch.vcxproj.filters b/substringSearch/substringSearch/substringSearch.vcxproj.filters new file mode 100644 index 0000000..49420f9 --- /dev/null +++ b/substringSearch/substringSearch/substringSearch.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/substringSearch/substringSearch/test.txt b/substringSearch/substringSearch/test.txt new file mode 100644 index 0000000..051b022 --- /dev/null +++ b/substringSearch/substringSearch/test.txt @@ -0,0 +1 @@ +I go home. ererrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrer ere rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrre erererer erettttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt erererer eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeea \ No newline at end of file From dfacc309fbbb42a6c12e0ae806523153eaedc859 Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 11 Dec 2022 19:28:26 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D0=B2?= =?UTF-8?q?=20=D0=9A=D0=9C=D0=9F=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D1=91=D1=81=20=D0=9A=D0=9C=D0=9F=20=D0=B2=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D1=83=D0=BB=D1=8C,=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- substringSearch/substringSearch/KMP.c | 47 +++++++++++++ substringSearch/substringSearch/KMP.h | 4 ++ substringSearch/substringSearch/main.c | 69 +++++-------------- .../substringSearch/substringSearch.vcxproj | 6 ++ .../substringSearch.vcxproj.filters | 14 ++++ substringSearch/substringSearch/test.c | 15 ++++ substringSearch/substringSearch/test.h | 5 ++ substringSearch/substringSearch/test.txt | 2 +- 8 files changed, 111 insertions(+), 51 deletions(-) create mode 100644 substringSearch/substringSearch/KMP.c create mode 100644 substringSearch/substringSearch/KMP.h create mode 100644 substringSearch/substringSearch/test.c create mode 100644 substringSearch/substringSearch/test.h diff --git a/substringSearch/substringSearch/KMP.c b/substringSearch/substringSearch/KMP.c new file mode 100644 index 0000000..eaa1092 --- /dev/null +++ b/substringSearch/substringSearch/KMP.c @@ -0,0 +1,47 @@ +#include "KMP.h" +#include +#include +#include + +int* prefixFunction(const char* string) { + int* prefixArray = calloc(strlen(string), sizeof(int)); + if (prefixArray == NULL) { + return NULL; + } + prefixArray[0] = 0; + size_t sizeString = strlen(string); + for (size_t i = 1, j = 0; i < sizeString; ++i) { + while (j > 0 && string[j] != string[i]) { + j = prefixArray[j - 1]; + } + if (string[i] == string[j]) { + ++j; + } + prefixArray[i] = j; + } + return prefixArray; +} + +int findIndexSubstring(const char* string, const char* substring, int* errorCode) { + size_t sizeString = strlen(string); + size_t sizeSubstring = strlen(substring); + int result = -1; + if (sizeString == 0 || sizeSubstring == 0 || sizeString < sizeSubstring) { + return -1; + } + int* prefixArray = prefixFunction(substring); + for (int i = 0, j = 0; i < sizeString; ++i) { + while (j > 0 && substring[j] != string[i]) { + j = prefixArray[j - 1]; + } + if (substring[j] == string[i]) { + ++j; + } + if (j == sizeSubstring) { + free(prefixArray); + return i - j + 1; + } + } + free(prefixArray); + return -1; +} diff --git a/substringSearch/substringSearch/KMP.h b/substringSearch/substringSearch/KMP.h new file mode 100644 index 0000000..c9acd43 --- /dev/null +++ b/substringSearch/substringSearch/KMP.h @@ -0,0 +1,4 @@ +#pragma once + +//the KMP algorithm for finding a substring in a string +int findIndexSubstring(const char* string, const char* substring, int* errorCode); \ No newline at end of file diff --git a/substringSearch/substringSearch/main.c b/substringSearch/substringSearch/main.c index 0eef95a..c3bf474 100644 --- a/substringSearch/substringSearch/main.c +++ b/substringSearch/substringSearch/main.c @@ -1,3 +1,5 @@ +#include "test.h" +#include "KMP.h" #include #include #include @@ -36,49 +38,6 @@ char* getLine(int* lengthString, int* errorCode) { return string; } -int* prefixFunction(const char* string) { - int* prefixArray = calloc(strlen(string), sizeof(int)); - if (prefixArray == NULL) { - return NULL; - } - prefixArray[0] = 0; - size_t sizeString = strlen(string); - for (size_t i = 1, j = 0; i < sizeString; ++i) { - while (j > 0 && string[j] != string[i]) { - j = prefixArray[j - 1]; - } - if (string[i] == string[j]) { - ++j; - } - prefixArray[i] = j; - } - return prefixArray; -} - -int findIndexSubstring(const char* string, const char* substring, int* errorCode) { - size_t sizeString = strlen(string); - size_t sizeSubstring = strlen(substring); - int result = -1; - if (sizeString == 0 || sizeSubstring == 0 || sizeString < sizeSubstring) { - return -1; - } - int* prefixArray = prefixFunction(substring); - for (int i = 0, j = 0; i < sizeString; ++i) { - while (j > 0 && prefixArray[j] != prefixArray[i]) { - j = prefixArray[j - 1]; - } - if (prefixArray[j] == prefixArray[i]) { - ++j; - } - if (j == sizeSubstring) { - free(prefixArray); - return i - j + 1; - } - } - free(prefixArray); - return -1; -} - int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const char* substring) { FILE* file; *errorCode = fopen_s(&file, nameFile, "r"); @@ -92,22 +51,27 @@ int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const int steps = 1; size_t sizeBuffer = 0; size_t sizeCopyText = 0; + int index = 0; + int indexToReturn = 0; while (fgets(copyText, 200, file) != NULL) { switch (steps) { case 1: + index = findIndexSubstring(copyText, substring, errorCode); strcpy(buffer, copyText); steps = 2; break; case 2: sizeBuffer = strlen(buffer); sizeCopyText = strlen(copyText); - for (int i = sizeBuffer; i < sizeCopyText; ++i) { - buffer[i] = copyText[i]; + for (int i = sizeBuffer; i < sizeBuffer + sizeCopyText; ++i) { + buffer[i] = copyText[i - 199]; } steps = 3; + index = findIndexSubstring(buffer, substring, errorCode); break; case 3: + indexToReturn += 199; sizeBuffer = strlen(buffer); if (sizeBuffer >= 200) { int difference = sizeBuffer - 199; @@ -116,23 +80,28 @@ int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const } } sizeCopyText = strlen(copyText); - for (int i = 0; i < copyText; ++i) { - buffer[i + 200] = copyText[i]; + for (int i = 0; i < sizeCopyText; ++i) { + buffer[i + 199] = copyText[i]; } + index = findIndexSubstring(buffer, substring, errorCode); break; } - int index = findIndexSubstring(buffer, substring, errorCode); if (index != -1) { - return index; + return index + indexToReturn; } if (*errorCode != 0) { return -1; } - memset(copyText, 0, 200); + memset(copyText, 0, 200); } } int main() { + if (test()) { + printf("Test correct!\n"); + } else { + printf("Test not correct!\n"); + } printf("Input file name with extension\n"); int errorCode = 0; int sizeFile = 0; diff --git a/substringSearch/substringSearch/substringSearch.vcxproj b/substringSearch/substringSearch/substringSearch.vcxproj index 9f10ac3..fc93fba 100644 --- a/substringSearch/substringSearch/substringSearch.vcxproj +++ b/substringSearch/substringSearch/substringSearch.vcxproj @@ -139,11 +139,17 @@ + + + + + + diff --git a/substringSearch/substringSearch/substringSearch.vcxproj.filters b/substringSearch/substringSearch/substringSearch.vcxproj.filters index 49420f9..49d7380 100644 --- a/substringSearch/substringSearch/substringSearch.vcxproj.filters +++ b/substringSearch/substringSearch/substringSearch.vcxproj.filters @@ -18,10 +18,24 @@ Исходные файлы + + Исходные файлы + + + Исходные файлы + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + \ No newline at end of file diff --git a/substringSearch/substringSearch/test.c b/substringSearch/substringSearch/test.c new file mode 100644 index 0000000..45aa14d --- /dev/null +++ b/substringSearch/substringSearch/test.c @@ -0,0 +1,15 @@ +#include "test.h" +#include "KMP.h" + +bool test() { + int errorCode = 0; + bool itIsOK = findIndexSubstring("error", "ror", &errorCode) == 2; + if (errorCode != 0 || !itIsOK) { + return false; + } + itIsOK = findIndexSubstring("error", "a", &errorCode) == -1; + if (errorCode != 0 || !itIsOK) { + return false; + } + return findIndexSubstring("error", "rortutuut", &errorCode) == -1 && errorCode == 0; +} \ No newline at end of file diff --git a/substringSearch/substringSearch/test.h b/substringSearch/substringSearch/test.h new file mode 100644 index 0000000..4793ad5 --- /dev/null +++ b/substringSearch/substringSearch/test.h @@ -0,0 +1,5 @@ +#pragma once +#include + +//Tests for find position substring +bool test(void); \ No newline at end of file diff --git a/substringSearch/substringSearch/test.txt b/substringSearch/substringSearch/test.txt index 051b022..70bff31 100644 --- a/substringSearch/substringSearch/test.txt +++ b/substringSearch/substringSearch/test.txt @@ -1 +1 @@ -I go home. ererrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrer ere rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrre erererer erettttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt erererer eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeea \ No newline at end of file +I go home. ereadsbdfbgdsfdiofjsofijw9uhgwglkfdfijsfierrrrrrrrrrrrrrrrrrrrrrrrrrrer ere rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrre erererer erettttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt erererer eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeap \ No newline at end of file From 00c8a045a57865eabdbff278dc888991137d4c43 Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 12 Dec 2022 08:21:11 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D0=B0=20=D0=B8=20=D0=BE=D1=87=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=BA=D1=83=20=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- substringSearch/substringSearch/main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/substringSearch/substringSearch/main.c b/substringSearch/substringSearch/main.c index c3bf474..860d25e 100644 --- a/substringSearch/substringSearch/main.c +++ b/substringSearch/substringSearch/main.c @@ -87,13 +87,16 @@ int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const break; } if (index != -1) { + fclose(file); return index + indexToReturn; } if (*errorCode != 0) { + fclose(file); return -1; } memset(copyText, 0, 200); } + fclose(file); } int main() { @@ -115,6 +118,7 @@ int main() { char* substring = getLine(&sizeSubstring, &errorCode); if (errorCode != 0 || substring == NULL) { printf("Error\n"); + free(nameFile); return -1; } while (sizeSubstring > 100) { @@ -123,12 +127,15 @@ int main() { substring = getLine(&sizeSubstring, &errorCode); if (errorCode != 0 || substring == NULL) { printf("Error\n"); + free(nameFile); return -1; } } int number = workWithFile(nameFile, &errorCode, sizeSubstring, substring); if (errorCode != 0) { printf("Error\n"); + free(nameFile); + free(substring); return -1; } free(nameFile); From 99c9e279788eea698b2f36e0e356f661684300ac Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 23 Dec 2022 18:16:19 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- substringSearch/substringSearch/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substringSearch/substringSearch/main.c b/substringSearch/substringSearch/main.c index 860d25e..51ca3ed 100644 --- a/substringSearch/substringSearch/main.c +++ b/substringSearch/substringSearch/main.c @@ -39,11 +39,11 @@ char* getLine(int* lengthString, int* errorCode) { } int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const char* substring) { - FILE* file; + FILE* file = NULL; *errorCode = fopen_s(&file, nameFile, "r"); if (*errorCode != 0 || file == NULL) { *errorCode = -1; - return; + return -1; } char buffer[400] = { '\0' };