Repository navigation
Expand file tree
/
Copy pathread_task_list.c
More file actions
157 lines (139 loc) · 5.31 KB
/
Copy pathread_task_list.c
File metadata and controls
157 lines (139 loc) · 5.31 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct {
char time[13]; // save year, month, day, hour with concat
char filename[51];
char contents[201];
char group_user[51];
char category[20];
char schedule_check[20];
char flag[5];
} task;
task task_list[30];
task *read_dir(char *);
int main() {
char *path_ptr = "./choi";
task *Task_list; // Task_list receiving task_list from read_dir
Task_list = read_dir(path_ptr);
free(Task_list);
return 0;
}
int check_file_exists(char *directory_path, char *checkfile) {
char file_path[256];
memset(file_path, 0, sizeof(file_path));
strcat(file_path, directory_path);
strcat(file_path, checkfile);
FILE *file = fopen(file_path, "r");
if (file) {
fclose(file);
return 1; // 파일이 존재하는 경우
} else {
return 0; // 파일이 존재하지 않는 경우
}
}
task *read_dir(char *dir_path) {
DIR *dir;
task *task_list = malloc(sizeof(task) * 30);
FILE *temp;
struct dirent *filelist;
struct stat info;
int fd;
int i = 0;
int grouplen;
if ((dir = opendir(dir_path)) == NULL) { // opendir
perror("cannot open directory");
exit(1);
}
while ((filelist = readdir(dir)) != NULL) { // readdir
if (filelist->d_type == DT_REG) {
/* make filepath */
char filepath[256];
memset(filepath, 0, sizeof(filepath));
char filetxtname[50];
memset(filetxtname, 0, sizeof(filetxtname));
strcat(filetxtname, filelist->d_name);
strcat(filetxtname, ".txt");
printf("%s\n", filetxtname);
fflush(stdout);
if (strcpy(task_list[i].filename, filelist->d_name) ==
NULL) { // save filename
perror("save filename error");
exit(1);
}
if (check_file_exists("./Today/School/", filetxtname) == 1) {
strcpy(task_list[i].category, "School");
strcpy(task_list[i].schedule_check, "Today");
} else if (check_file_exists("./Today/Family/", filetxtname) == 1) {
strcpy(task_list[i].category, "Family");
strcpy(task_list[i].schedule_check, "Today");
} else if (check_file_exists("./Today/Friend/", filetxtname) == 1) {
strcpy(task_list[i].category, "Friend");
strcpy(task_list[i].schedule_check, "Today");
} else if (check_file_exists("./Finished/School/", filetxtname) ==
1) {
strcpy(task_list[i].category, "School");
strcpy(task_list[i].schedule_check, "Finished");
} else if (check_file_exists("./Finished/Family/", filetxtname) ==
1) {
strcpy(task_list[i].category, "Family");
strcpy(task_list[i].schedule_check, "Finished");
} else if (check_file_exists("./Finished/Friend/", filetxtname) ==
1) {
strcpy(task_list[i].category, "Friend");
strcpy(task_list[i].schedule_check, "Finished");
} else if (check_file_exists("./Scheduled/School/", filetxtname) ==
1) {
strcpy(task_list[i].category, "School");
strcpy(task_list[i].schedule_check, "Scheduled");
} else if (check_file_exists("./Scheduled/Family/", filetxtname) ==
1) {
strcpy(task_list[i].category, "Family");
strcpy(task_list[i].schedule_check, "Scheduled");
} else {
strcpy(task_list[i].category, "Friend");
strcpy(task_list[i].schedule_check, "Scheduled");
}
printf("%s %s\n", task_list[i].category,
task_list[i].schedule_check);
strcpy(filepath, dir_path);
strcat(filepath, "/");
strcat(filepath, filelist->d_name);
FILE *file = fopen(filepath, "r");
printf("%s", filelist->d_name);
if (file == NULL) {
printf("파일을 열 수 없습니다.\n");
}
if (fgets(task_list[i].time, sizeof(task_list[i].time), file) ==
NULL) {
printf("파일을 읽을 수 없습니다.\n");
fclose(file);
}
printf("%s\n", task_list[i].time);
if (fgets(task_list[i].flag, sizeof(task_list[i].flag), file) ==
NULL) {
printf("파일을 읽을 수 없습니다.\n");
fclose(file);
}
printf("%s\n", task_list[i].flag);
if (fgets(task_list[i].group_user, sizeof(task_list[i].group_user),
file) == NULL) {
printf("파일을 읽을 수 없습니다.\n");
fclose(file);
}
printf("%s\n", task_list[i].group_user);
if (fgets(task_list[i].contents, sizeof(task_list[i].contents),
file) == NULL) {
printf("파일을 읽을 수 없습니다.\n");
fclose(file);
}
i++;
}
}
closedir(dir);
return task_list;
}