-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
136 lines (95 loc) · 2.7 KB
/
main.c
File metadata and controls
136 lines (95 loc) · 2.7 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "include/simple-stdio.h"
void consumeRest() {
char c;
do {
c = simpleGetchar();
} while (c != '\n' && c != 0);
}
int main(void) {
simplePuts("Hello, World!");
simplePutchar('A');
simplePutchar('\n');
char *string = "Enter a character: \0";
for (int i = 0; string[i] != '\0'; i++)
simplePutchar(string[i]);
int c = simpleGetchar();
simplePutchar(c);
simplePutchar('\n');
consumeRest();
int file = simpleFopen("example.txt", 'w');
if (file < 0) {
simplePuts("Error occured while opening file.");
return 1;
}
simplePutc('H', file);
simpleFputc('i', file);
simplePutc('!', file);
simpleFclose(file);
file = simpleFopen("example.txt" , 'r');
if (file < 0) {
simplePuts("Error occured while opening file.");
return 1;
}
c = simpleFgetc(file);
while (c != -1 && c != '\n') {
simplePutc(c, simpleStdout);
c = simpleGetc(file);
}
simpleFclose(file);
simplePutc('\n', 1);
char buff[16];
if (simpleFgets(buff, 16 , 0) != (void*)0) { // (void*)0 == NULL
for (int i = 0; buff[i]; i++) simplePutc(buff[i], simpleStdout);
}
char FileBuff[8];
file = simpleFopen("example.txt" , 'r');
if (file < 0) {
simplePuts("Failed to open file.");
return 1;
}
if (simpleFgets(FileBuff, 8, file) != (void*)0) {
for (int i = 0; FileBuff[i]; i++) simplePutc(FileBuff[i], simpleStdout);
}
simpleFclose(file);
simplePutc('\n', simpleStdout);
file = simpleFopen("example.txt" , 'a');
if (file < 0) {
simplePuts("Failed to open file.");
return 1;
}
if (simpleFputs(" Writing using simple-stdio!\n" , file) < 0) {
simplePuts("Failed to write to file!");
}
simpleFclose(file);
file = simpleFopen("example.txt", 'r');
if (file < 0) {
simplePuts("Failed to open file.");
return 1;
}
int currPos = simpleFtell(file);
simplePutc((char)(currPos + 48) , simpleStdout); // print converted digit.
c = simpleGetc(file);
simplePutc('\n', simpleStdout);
currPos = simpleFtell(file);
simplePutc((char)(currPos + 48) , simpleStdout); // print converted digit.
simplePutc('\n', simpleStdout);
currPos = simpleFseek(file , 5 , 0);
if (currPos == -1) {
simplePuts("Failed to change file pos!");
simpleFclose(file);
return 1;
}
simplePutc((char)(currPos + 48) , simpleStdout); // print converted digit.
simplePutc('\n', simpleStdout);
simplePutc(simpleGetc(file) , simpleStdout);
simplePutc('\n' , simpleStdout);
simpleFclose(file);
simpleRename("example.txt" , "example_r.txt");
if (simpleRemove("example_r.txt") != 0) {
simplePuts("Failed to remove file!");
}
char buf[16];
simpleGetline(buf, 16, simpleStdin);
simplePuts(buf);
return 0;
}