From 666521d1cdabda46e14e1dff7d384678b41e260a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 18:47:46 +0500 Subject: [PATCH 1/2] writing a function --- Comments/Comments.sln | 31 +++++ Comments/Comments/Comments.c | 74 ++++++++++ Comments/Comments/Comments.h | 3 + Comments/Comments/Comments.txt | 3 + Comments/Comments/Comments.vcxproj | 153 +++++++++++++++++++++ Comments/Comments/Comments.vcxproj.filters | 36 +++++ Comments/Comments/Main.c | 38 +++++ Comments/Comments/Test.txt | 3 + Comments/Comments/TestComments.c | 19 +++ Comments/Comments/TestComments.h | 4 + 10 files changed, 364 insertions(+) create mode 100644 Comments/Comments.sln create mode 100644 Comments/Comments/Comments.c create mode 100644 Comments/Comments/Comments.h create mode 100644 Comments/Comments/Comments.txt create mode 100644 Comments/Comments/Comments.vcxproj create mode 100644 Comments/Comments/Comments.vcxproj.filters create mode 100644 Comments/Comments/Main.c create mode 100644 Comments/Comments/Test.txt create mode 100644 Comments/Comments/TestComments.c create mode 100644 Comments/Comments/TestComments.h diff --git a/Comments/Comments.sln b/Comments/Comments.sln new file mode 100644 index 0000000..578a646 --- /dev/null +++ b/Comments/Comments.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Comments", "Comments\Comments.vcxproj", "{74438356-D32C-4392-88FB-89F3F08A9915}" +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 + {74438356-D32C-4392-88FB-89F3F08A9915}.Debug|x64.ActiveCfg = Debug|x64 + {74438356-D32C-4392-88FB-89F3F08A9915}.Debug|x64.Build.0 = Debug|x64 + {74438356-D32C-4392-88FB-89F3F08A9915}.Debug|x86.ActiveCfg = Debug|Win32 + {74438356-D32C-4392-88FB-89F3F08A9915}.Debug|x86.Build.0 = Debug|Win32 + {74438356-D32C-4392-88FB-89F3F08A9915}.Release|x64.ActiveCfg = Release|x64 + {74438356-D32C-4392-88FB-89F3F08A9915}.Release|x64.Build.0 = Release|x64 + {74438356-D32C-4392-88FB-89F3F08A9915}.Release|x86.ActiveCfg = Release|Win32 + {74438356-D32C-4392-88FB-89F3F08A9915}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D6FB2DF2-D9A4-4AF2-84A4-E6846E85A292} + EndGlobalSection +EndGlobal diff --git a/Comments/Comments/Comments.c b/Comments/Comments/Comments.c new file mode 100644 index 0000000..4625cb2 --- /dev/null +++ b/Comments/Comments/Comments.c @@ -0,0 +1,74 @@ +#include "Comments.h" +#include "testComments.h" +#include +#include + +char* readBytes(const char* filename, int* error) +{ + FILE* file = fopen(filename, "r"); + if (file == NULL) + { + *error = 1; + return NULL; + } + int counter = 0; + while (!feof(file)) + { + char* buffer = (char*)calloc(1, sizeof(char)); + { + if (buffer == NULL) + { + *error = 2; + return NULL; + } + } + const int readBytes = fscanf(file, "%c", buffer); + if (readBytes < 0) + { + break; + } + counter++; + free(buffer); + } + fclose(file); + file = fopen(filename, "r"); + char* data = calloc(counter, sizeof(char)); + if (data == NULL) + { + *error = 2; + return NULL; + } + int linesRead = 0; + while (!feof(file)) + { + char* buffer = (char*)calloc(1, sizeof(char)); + { + if (buffer == NULL) + { + *error = 2; + return NULL; + } + } + const int readBytes = fscanf(file, "%c", buffer); + if (readBytes < 0) + { + break; + } + if (*buffer == ';') + { + while (*buffer != '\n' && *buffer != '\0' && linesRead <= counter) + { + data[linesRead] = *buffer; + ++linesRead; + const int readBytes = fscanf(file, "%c", buffer); + if (readBytes < 0) + { + break; + } + } + } + free(buffer); + } + fclose(file); + return data; +} \ No newline at end of file diff --git a/Comments/Comments/Comments.h b/Comments/Comments/Comments.h new file mode 100644 index 0000000..33af030 --- /dev/null +++ b/Comments/Comments/Comments.h @@ -0,0 +1,3 @@ +#pragma once + +char* readBytes(const char* filename, int* error); diff --git a/Comments/Comments/Comments.txt b/Comments/Comments/Comments.txt new file mode 100644 index 0000000..c5a0759 --- /dev/null +++ b/Comments/Comments/Comments.txt @@ -0,0 +1,3 @@ +helloWorld;hello +;stackisempty +cceceececececec;list \ No newline at end of file diff --git a/Comments/Comments/Comments.vcxproj b/Comments/Comments/Comments.vcxproj new file mode 100644 index 0000000..f66430a --- /dev/null +++ b/Comments/Comments/Comments.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {74438356-d32c-4392-88fb-89f3f08a9915} + Comments + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _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/Comments/Comments/Comments.vcxproj.filters b/Comments/Comments/Comments.vcxproj.filters new file mode 100644 index 0000000..8c8259f --- /dev/null +++ b/Comments/Comments/Comments.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {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/Comments/Comments/Main.c b/Comments/Comments/Main.c new file mode 100644 index 0000000..d7a6434 --- /dev/null +++ b/Comments/Comments/Main.c @@ -0,0 +1,38 @@ +#include "Comments.h" +#include "testComments.h" +#include + +int main() +{ + if (!testComments()) + { + printf("Test failed"); + return -1; + } + int error = 0; + const char* data = readBytes("Comments.txt", &error); + if (error == 1) + { + free(data); + printf("File not found"); + return -1; + } + if (error == 2) + { + free(data); + printf("Memory not allocated"); + return -1; + } + printf("all comments :"); + int i = 0; + while (data[i] != '\0') + { + if (data[i] == ';') + { + printf("\n"); + } + printf("%c", data[i]); + i++; + } + free(data); +} \ No newline at end of file diff --git a/Comments/Comments/Test.txt b/Comments/Comments/Test.txt new file mode 100644 index 0000000..7c12ed3 --- /dev/null +++ b/Comments/Comments/Test.txt @@ -0,0 +1,3 @@ +asdsadasd;helloworld +;helloworld)) +wefewfewfe;kek \ No newline at end of file diff --git a/Comments/Comments/TestComments.c b/Comments/Comments/TestComments.c new file mode 100644 index 0000000..22016d2 --- /dev/null +++ b/Comments/Comments/TestComments.c @@ -0,0 +1,19 @@ +#include "testComments.h" +#include "Comments.h" + +bool testComments() +{ + int error = 0; + char* data = readBytes("Test.txt", &error); + int i = 0; + const char arrayOfSymbol[29] = ";helloworld;helloworld));kek"; + while (data[i] != '\0' && arrayOfSymbol[i] != '\0') + { + if (data[i] != arrayOfSymbol[i]) + { + return false; + } + i++; + } + return true; +} \ No newline at end of file diff --git a/Comments/Comments/TestComments.h b/Comments/Comments/TestComments.h new file mode 100644 index 0000000..d44ec16 --- /dev/null +++ b/Comments/Comments/TestComments.h @@ -0,0 +1,4 @@ +#pragma once +#include + +bool testComments(); From 63dd218e169588a44427a112ed561862f23c04a0 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 18:51:27 +0500 Subject: [PATCH 2/2] add comments --- Comments/Comments/Comments.c | 1 - Comments/Comments/Comments.h | 1 + Comments/Comments/TestComments.h | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Comments/Comments/Comments.c b/Comments/Comments/Comments.c index 4625cb2..33e9a0e 100644 --- a/Comments/Comments/Comments.c +++ b/Comments/Comments/Comments.c @@ -1,5 +1,4 @@ #include "Comments.h" -#include "testComments.h" #include #include diff --git a/Comments/Comments/Comments.h b/Comments/Comments/Comments.h index 33af030..959fffc 100644 --- a/Comments/Comments/Comments.h +++ b/Comments/Comments/Comments.h @@ -1,3 +1,4 @@ #pragma once +// Function for reading comments from a file char* readBytes(const char* filename, int* error); diff --git a/Comments/Comments/TestComments.h b/Comments/Comments/TestComments.h index d44ec16..48427cf 100644 --- a/Comments/Comments/TestComments.h +++ b/Comments/Comments/TestComments.h @@ -1,4 +1,5 @@ #pragma once #include +//A function for testing a function that reads comments from a file bool testComments();