-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-file.c
51 lines (43 loc) · 1.04 KB
/
07-file.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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
// create
int fd1 = creat("test 1.txt", 0642);
if (fd1 == -1) {
printf("Cannot create file!\n");
}
else {
printf("File created sucessfully\n");
// close
if (close(fd1) == -1)
printf("Cannot close file!\n");
else
printf("File closed sucessfully\n");
}
sleep(1);
// open
int fd2 = open("test 2.txt", O_CREAT|O_RDWR, 0642);
if (fd2 == -1)
printf("Cannot open file!\n");
else
printf("File opened sucessfully\n");
// write
char buffer1[] = "hello world";
write(fd2, buffer1, strlen(buffer1));
printf("Write: %s\n", buffer1);
// seek
lseek(fd2, 0, SEEK_SET);
// read
char buffer2[12];
read(fd2, buffer2, 11);
buffer2[11] = 0;
printf("Read: %s\n", buffer2);
// close
if (close(fd2) == -1)
printf("Cannot close file!\n");
else
printf("File closed sucessfully\n");
return 0;
}