-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf_interpreter.cpp
52 lines (44 loc) · 1018 Bytes
/
bf_interpreter.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
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <cstring>
int main(const int argc, char* argv[])
{
char path[32767];
char array[30000] = {0}, *ptr = array;
if (argc > 1)
{
strcpy_s(path, argv[1]);
}
else
{
fprintf(stderr, "Enter the path to the file: ");
}
scanf_s("%32767s", path, 32767);
FILE* bf_file = fopen(path, "r");
if (!bf_file)
{
fprintf(stderr, "Error: Could not open file %s\n", path);
return EXIT_FAILURE;
}
char line[1024];
while (fgets(line, 1024, bf_file))
{
for (const char i : line)
{
if (i == '>')
++ptr;
else if (i == '<')
--ptr;
else if (i == '+')
++*ptr;
else if (i == '-')
--*ptr;
else if (i == '.')
putchar(*ptr);
else if (i == ',')
(*ptr) = getchar();
}
}
}