-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpp
More file actions
67 lines (56 loc) · 1.24 KB
/
Copy pathio.cpp
File metadata and controls
67 lines (56 loc) · 1.24 KB
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
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "io.h"
char* create_array(const int text_bytes)
{
char* text_a = (char*)calloc(text_bytes, sizeof(char));
return text_a;
}
char** make_pointers_array(char* text_a, size_t line_count)
{
char** lines = (char**)calloc(line_count, sizeof(char*));
assert(lines != nullptr);
char* token = strtok(text_a, "\n");
size_t index = 0;
while (token != nullptr)
{
lines[index++] = token;
token = strtok(nullptr, "\n");
}
return lines;
}
void close_text(struct Text* text, struct Lines* line)
{
free(text->text_a);
free(line->lines);
fclose(text->file);
}
size_t count_lines(struct Text* text)
{
size_t line_count = 0;
for (size_t i = 0; i < text->text_bytes; i++)
{
if (text->text_a[i] == '\n')
{
line_count++;
}
}
return line_count;
}
const long return_size_text(FILE *text)
{
fseek(text, 0, SEEK_END);
long text_bytes = ftell(text);
rewind(text);
return text_bytes;
}
void print_results(struct Lines* line)
{
for (size_t i = 0; i < line->line_count; i++)
{
printf("%s\n", line->lines[i]);
}
}