커뮤니티 활동을 하다보니 과제를 물어보시는분이 있더라..
과제내용인 즉...
특정폴더(하위 디렉토리 내의 파일도 검사 해야 함)에 저장되어 있는 텍스트 파일(txt)의 내용을 검사하여 파일 내 저장 되어있는 “이메일주소”를 찾아 내는 프로그램을 만드시오.
C로 짰고 밥먹으면서 짰던거라 조금 코드가 더럽다.
윈도우용 dirent.h를 include해야한다.
http://www.mediafire.com/view/3ufjapid9foqu9q/dirent.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#include <stdio.h>
#include <sys/types.h>
#include "MyQueue.h"
#include "dirent.h"
#pragma warning(disable:4996)
myQueue mQueue;
static int
find_directory(
const char *dirname)
{
DIR *dir;
char buffer[PATH_MAX + 2];
char *p = buffer;
const char *src;
char *end = &buffer[PATH_MAX];
int ok;
char * extName;
/* Copy directory name to buffer */
src = dirname;
while (p < end && *src != '\0') {
*p++ = *src++;
}
*p = '\0';
/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
struct dirent *ent;
/* Print all files and directories within the directory */
while ((ent = readdir (dir)) != NULL) {
char *q = p;
char c;
/* Get final character of directory name */
if (buffer < q) {
c = q[-1];
} else {
c = ':';
}
/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\') {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
*q++ = '\\';
#else
*q++ = '/';
#endif
}
/* Append file name */
src = ent->d_name;
while (q < end && *src != '\0') {
*q++ = *src++;
}
*q = '\0';
/* Decide what to do with the directory entry */
switch (ent->d_type) {
case DT_REG:
/* Output file name with directory */
extName = strrchr(ent->d_name, '.');
if(extName == NULL) break;
if(strcmp(extName, ".txt") == 0)
{
//printf("fine txt file : %s\n", ent->d_name);
//printf ("%s\n", buffer);
enqueue(&mQueue, buffer);
}
break;
case DT_DIR:
/* Scan sub-directory recursively */
if (strcmp (ent->d_name, ".") != 0
&& strcmp (ent->d_name, "..") != 0) {
find_directory (buffer);
}
break;
default:
/* Do not device entries */
/*NOP*/;
}
}
closedir (dir);
ok = 1;
} else {
/* Could not open directory */
printf ("Cannot open directory %s\n", dirname);
ok = 0;
}
return ok;
}
void myDir(const char * dirPath)
{
DIR * dp;
struct dirent * ent;
char * extName;
dp = opendir(dirPath);
if(dp != NULL)
{
while(1)
{
ent = readdir(dp);
if(ent == NULL)
break;
extName = strrchr(ent->d_name, '.');
if(strcmp(extName, ".txt") == 0)
printf("fine txt file : %s\n", ent->d_name);
}
}
}
int IsAvailableEMail(const char * srcMail)
{
// 찾음 : 1
// 없음 : 0
int iAtCount = 0; //@ 위치
int iDotCount = 0; // . 위치
int i;
char * eMail = (char*)malloc( strlen(srcMail) + 1 );
strcpy(eMail, srcMail);
if(strcmp(eMail, "") == 0) return 0;
for(i = 0; i < strlen(eMail); i++)
{
if(i > 0 && eMail[i] == '@' ) iAtCount = i+1; // ①
if(iAtCount > 0 && i > iAtCount && eMail[i] == '.') iDotCount = i+1; // ②
}
free(eMail);
if(i > iDotCount && iAtCount > 0 && iDotCount > 0) return 1; // ③
else return 0;
}
int main()
{
//입력받은 디렉토리에서 확장자가 .txt인 파일을 찾고
//큐에 넣은 후 이메일 주소를 뽑아내는 구조
//큐 초기화
FILE * fp;
char buf[1024];
char * path;
const char * filePath;
initQueue(&mQueue);
//디렉토리 순회
find_directory("C:\\Mail");
////제대로 나오는지 출력
//while( !empty(&mQueue) )
//{
// printf("%s\n", frontQueue(&mQueue));
// deQueue(&mQueue);
//}
//
//찾은 파일을 열어서 이메일 주소를 확인
while( !empty(&mQueue) )
{
path = frontQueue(&mQueue);
filePath = path;
fp = fopen(filePath, "r");
if(fp != NULL)
{
int isEmail;
char * ch;
int i;
//while(fgets(buf, 1024, fp))
while( 0 < fscanf(fp, "%s", buf) )
{
//printf("%s", buf);
isEmail = IsAvailableEMail(buf);
if(isEmail)
{
ch = strchr(buf, '"');
if(ch != NULL)
*ch = ' ';
ch = strchr(buf, '(');
if(ch != NULL)
*ch = ' ';
ch = strchr(buf, ')');
if(ch != NULL)
*ch = ' ';
printf("%s\n", buf);
}
}
fclose(fp);
deQueue(&mQueue);
}
}
destroyQueue(&mQueue);
return 0;
}
|
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#include <stdio.h>
#include <sys/types.h>
#include "MyQueue.h"
#include "dirent.h"
#pragma warning(disable:4996)
myQueue mQueue;
static int
find_directory(
const char *dirname)
{
DIR *dir;
char buffer[PATH_MAX + 2];
char *p = buffer;
const char *src;
char *end = &buffer[PATH_MAX];
int ok;
char * extName;
/* Copy directory name to buffer */
src = dirname;
while (p < end && *src != '\0') {
*p++ = *src++;
}
*p = '\0';
/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
struct dirent *ent;
/* Print all files and directories within the directory */
while ((ent = readdir (dir)) != NULL) {
char *q = p;
char c;
/* Get final character of directory name */
if (buffer < q) {
c = q[-1];
} else {
c = ':';
}
/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\') {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
*q++ = '\\';
#else
*q++ = '/';
#endif
}
/* Append file name */
src = ent->d_name;
while (q < end && *src != '\0') {
*q++ = *src++;
}
*q = '\0';
/* Decide what to do with the directory entry */
switch (ent->d_type) {
case DT_REG:
/* Output file name with directory */
extName = strrchr(ent->d_name, '.');
if(extName == NULL) break;
if(strcmp(extName, ".txt") == 0)
{
//printf("fine txt file : %s\n", ent->d_name);
//printf ("%s\n", buffer);
enqueue(&mQueue, buffer);
}
break;
case DT_DIR:
/* Scan sub-directory recursively */
if (strcmp (ent->d_name, ".") != 0
&& strcmp (ent->d_name, "..") != 0) {
find_directory (buffer);
}
break;
default:
/* Do not device entries */
/*NOP*/;
}
}
closedir (dir);
ok = 1;
} else {
/* Could not open directory */
printf ("Cannot open directory %s\n", dirname);
ok = 0;
}
return ok;
}
void myDir(const char * dirPath)
{
DIR * dp;
struct dirent * ent;
char * extName;
dp = opendir(dirPath);
if(dp != NULL)
{
while(1)
{
ent = readdir(dp);
if(ent == NULL)
break;
extName = strrchr(ent->d_name, '.');
if(strcmp(extName, ".txt") == 0)
printf("fine txt file : %s\n", ent->d_name);
}
}
}
int IsAvailableEMail(const char * srcMail)
{
// 찾음 : 1
// 없음 : 0
int iAtCount = 0; //@ 위치
int iDotCount = 0; // . 위치
int i;
char * eMail = (char*)malloc( strlen(srcMail) + 1 );
strcpy(eMail, srcMail);
if(strcmp(eMail, "") == 0) return 0;
for(i = 0; i < strlen(eMail); i++)
{
if(i > 0 && eMail[i] == '@' ) iAtCount = i+1; // ①
if(iAtCount > 0 && i > iAtCount && eMail[i] == '.') iDotCount = i+1; // ②
}
free(eMail);
if(i > iDotCount && iAtCount > 0 && iDotCount > 0) return 1; // ③
else return 0;
}
int main()
{
//입력받은 디렉토리에서 확장자가 .txt인 파일을 찾고
//큐에 넣은 후 이메일 주소를 뽑아내는 구조
//큐 초기화
FILE * fp;
char buf[1024];
char * path;
const char * filePath;
initQueue(&mQueue);
//디렉토리 순회
find_directory("C:\\Mail");
////제대로 나오는지 출력
//while( !empty(&mQueue) )
//{
// printf("%s\n", frontQueue(&mQueue));
// deQueue(&mQueue);
//}
//
//찾은 파일을 열어서 이메일 주소를 확인
while( !empty(&mQueue) )
{
path = frontQueue(&mQueue);
filePath = path;
fp = fopen(filePath, "r");
if(fp != NULL)
{
int isEmail;
char * ch;
int i;
//while(fgets(buf, 1024, fp))
while( 0 < fscanf(fp, "%s", buf) )
{
//printf("%s", buf);
isEmail = IsAvailableEMail(buf);
if(isEmail)
{
ch = strchr(buf, '"');
if(ch != NULL)
*ch = ' ';
ch = strchr(buf, '(');
if(ch != NULL)
*ch = ' ';
ch = strchr(buf, ')');
if(ch != NULL)
*ch = ' ';
printf("%s\n", buf);
}
}
fclose(fp);
deQueue(&mQueue);
}
}
destroyQueue(&mQueue);
return 0;
}
|
실행결과 |
조금 어설프긴 하지만 돌아가는거에서 만족..!
댓글 없음:
댓글 쓰기