-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.c
59 lines (51 loc) · 1.22 KB
/
main.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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
int ret;
ret = rmdir("filetest");
ret = mkdir("filetest", 0);
int fd = open("filetest/a.txt", O_RDWR | O_CREAT);
if (fd == -1) {
perror("can not create the file\n");
return -1;
}
puts("rmdir, mkdir, open success!");
FILE *fp = fdopen(fd, "w");
fprintf(fp, "1 2 3 4\n");
fprintf(fp, "5 6 7 8\n");
fclose(fp);
char s[50];
fd = open("filetest/a.txt", O_RDWR);
fp = fdopen(fd, "r");
fgets(s, 50, fp);
if (strcmp("1 2 3 4\n", s)) {
perror("fdopen and freopen failed");
return -1;
}
puts("first fgets success!");
fgets(s, 50, fp);
if (strcmp("5 6 7 8\n", s)) {
perror("fdopen and freopen failed");
return -1;
}
puts("second fgets success!");
fclose(fp);
ret = remove("filetest/a.txt");
if (ret == -1) {
perror("remove file error");
return -1;
}
ret = remove("filetest");
if (ret == -1) {
perror("remove dir error");
return -1;
}
puts("remove file and dir success!");
puts("filetest success!");
return 0;
}