forked from OmerNachshon/VersionControlSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
162 lines (139 loc) · 4.25 KB
/
test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "files.h"
#include <stdio.h>
int main()
{
// file open
char filepath[] = "txt";
File* file = open_file(filepath,0,0);
printf("== open file ==\n");
printf("fd is %d\n", file->fd);
printf("Absolute path is: %s\n", file->absolutePath);
printf("Relative path is: %s\n", file->relativePath);
printf("Mode is: %d\n", file->mode);
printf("Seek is: %d\n", file->seek);
printf("Filesize is: %lld\n", file->fileSize);
// read next line
printf("== read first line ==\n");
printf("First line is: \n");
char* line = read_next_line(file);
printf("%s", line);
// read second line
printf("== read second line ==\n");
printf("Second line is: \n");
line = read_next_line(file);
printf("%s", line);
// no third line - check line == NULL
printf("== read third line ==\n");
printf("Third line is: \n");
line = read_next_line(file);
if ( line == 0 ) printf("EOF\n");
else printf("%s", line);
// no fourth line - condition is_eof
printf("== read fourth line ==\n");
printf("Fourth line is: \n");
if ( !is_eof(file) )
{
// printf("in eof\n");
line = read_next_line(file);
//printf("%s\n", line);
}else{
printf("EOF\n");
}
// file close
printf("== close file ==\n");
close_file(file);
printf("File is closed\n");
// open file again
printf("== read all the lines with read_lines ==\n");
printf("Openning file txt\n");
file = open_file(filepath,0,0);
// reading lines
printf("reading lines\n");
int size = 0;
char** lines = read_lines(file, &size);
printf("%d\n", size);
for(int i = 0; i < size; i ++ ){
printf("line %2d: %s", (i+1), lines[i]);
}
// closing file
close_file(file);
printf("File is closed\n");
// read files is loop - using read_next_line and is_eof
printf("== read lines with read_next_line and is_eof ==\n");
// open file again
printf("Openning file txt\n");
file = open_file(filepath,0,0);
int j = 1;
while(!is_eof(file))
{
line = read_next_line(file);
printf("line %2d: %s", j, line);
j++;
}
// close file
close_file(file);
printf("File is closed\n");
// write to file - no permissions
printf("== write with read mode and no permissions ==\n");
char str[] = "ghijk";
file = open_file(filepath,0,0);
int i = write_line(file, str);
printf("number of chars %d\n", i);
printf("== write with in rw mode and 666 perms ==\n");
int read_write = O_RDWR | O_CREAT;
int mode = 0666;
file = open_file(filepath,read_write,mode);
printf("file opened with rw+create, mode rw for e1\n");
i = write_line(file, str);
printf("number of chars %d\n", i);
// close file
close_file(file);
printf("File is closed\n");
// append to file
printf("== write with in a mode and 666 perms ==\n");
read_write = O_WRONLY | O_CREAT | O_APPEND;
mode = 0666;
file = open_file(filepath,read_write,mode);
printf("file opened with flags append, mode rw for e1\n");
char str2[] = "jklmn";
i = write_line(file, str2);
printf("number of chars %d\n", i);
// close file
close_file(file);
printf("File is closed\n");
// seek 5 forward
printf("== seek 6 from the start ==\n");
file = open_file(filepath, O_RDWR | O_CREAT, mode);
int result = seek_file(file, 6, 1);
printf("Seek result: %d\n", result);
printf("File seek: %d\n", file->seek);
line = read_next_line(file);
printf("%s", line);
// close file
close_file(file);
printf("File is closed\n");
// seek -6 from the end
printf("== seek 6 from the end ==\n");
file = open_file(filepath, O_RDWR | O_CREAT, mode);
result = seek_file(file, -6, 2);
printf("Seek result: %d\n", result);
printf("File seek: %d\n", file->seek);
line = read_next_line(file);
printf("%s", line);
// close file
close_file(file);
printf("File is closed\n");
// seek 6 from the second line
printf("== seek 6 after reading a line ==\n");
file = open_file(filepath, O_RDWR | O_CREAT, mode);
line = read_next_line(file);
result = seek_file(file, 6, 1);
printf("Seek result: %d\n", result);
printf("File seek: %d\n", file->seek);
line = read_next_line(file);
printf("%s", line);
// close file
close_file(file);
printf("File is closed\n");
return 0;
}