-
Notifications
You must be signed in to change notification settings - Fork 0
/
oocd.c
116 lines (105 loc) · 2.47 KB
/
oocd.c
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
#include<stdio.h>
#include<stdlib.h>
struct file{
/*__repr__()
__init__()*
read()*
filename*
seek()
truncate()*
close()*
write()*
read()**/
FILE * ptr;//file pointer
//char * (*center)(char * a, int b)
const char * (*read)(FILE * f);//read entire file
void * (*close)(FILE * f);
void * (*truncate)(struct file * f);
void * (*write)(FILE * f, const char * string);
const char * filename;
const char * mode;
const char * __repr__;
};
FILE * open(const char * filename, const char * mode, struct file * f);
const char * read(FILE * f);
const char * read_err(FILE * f);
void * close(FILE * f);
void * truncate(struct file * f);
void __init__(struct file * f, const char * mode, const char * filename);
void * write(FILE * f, const char * string);
void * write_err(FILE * f, const char * string);
int main(){
struct file f;
FILE*file;
const char * string;
file = open("C:/Users/nayja/Desktop/vid.mp4", "rb", &f);
string = f.read(file);
printf("%s", string);
f.close(file);
}
void * truncate(struct file * f){
fclose(f -> ptr);
FILE*ptr = fopen(f -> filename, "w");
if(ptr == NULL){
printf("File Not Found!");
}
else{
fprintf(ptr, "%s", "");
fclose(ptr);
}
f -> ptr = fopen(f -> filename, f -> mode);
}
void * write(FILE * f, const char * string){
fprintf(f, "%s", string);
}
void * write_err(FILE * f, const char * string){
printf("\nCannot Write To File In Read Mode!\n");
}
void __init__(struct file * f, const char * mode, const char * filename){
if(mode == "wb" || mode == "w"){
f -> read = read_err;
}
else{
f -> read = read;
}
f -> close = close;
if(mode == "rb" || mode == "r"){
f -> write = write_err;
}
else{
f -> write = write;
}
f -> truncate = truncate;
f -> mode = mode;
f -> __repr__ = "\n[__init__(), __repr__, close(), filename, read(), truncate(), write()]\n";
f -> filename = filename;
}
void * close(FILE * f){
fclose(f);
}
const char * read(FILE * f){
char * c;
long length;
if(f == NULL){
printf("File Not Found!");
exit(1);
}
else{
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
c = malloc(length);
if(c){
fread(c, 1, length, f);
}
}
return c;
}
const char * read_err(FILE * f){
printf("Cannot Read From File In Write Mode!");
}
FILE * open(const char * filename, const char * mode, struct file * f){
FILE*ptr = fopen(filename, mode);
__init__(f, mode, filename);
return ptr;
}