-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText.cpp
57 lines (44 loc) · 1.04 KB
/
Text.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Text.h"
#include "main.h"
void ReadTxt(char** text, const char* file_name)
{
assert(text);
assert(file_name);
FILE* file = fopen(file_name, "r");
if (file == NULL)
{
printf("[Input error] Unable to open file \"%s\"\n", file_name);
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
size_t num_symbols = ftell(file);
fseek(file, 0, SEEK_SET);
*text = (char*)calloc(num_symbols + 2, sizeof(**text));
if (*text == NULL)
{
printf("[Error] Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
**text = '\0';
fread(*text + 1, sizeof(**text), num_symbols, file);
fclose(file);
return;
}
int CountSymbols(char* text, const char str)
{
assert (text);
int counter = 0;
if (*text == str) counter++;
while (text = strchr(text + 1, str)) counter++;
return counter;
}
void PrintTxt (Line *lines, int num_lines, const char* file_name)
{
assert (lines);
assert (num_lines > 0);
FILE* file_out = fopen (file_name, "w");
for (int i = 0; i < num_lines; i++)
fprintf (file_out, "%s\n", lines[i].point);
fclose (file_out);
return;
}