#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include<time.h>
void
logman(
char
*ori_file,
char
*ori_word,
int
wordcnt)
{
FILE
*log_fp;
int
s_month,s_day,s_min,s_hour;
time_t
today;
struct
tm
*t;
today =
time
(NULL);
t =
localtime
(&today);
s_month=t->tm_mon+1;
s_day=t->tm_mday;
s_min=t->tm_min;
s_hour=t->tm_hour;
log_fp =
fopen
(
"log.txt"
,
"a+"
);
if
(log_fp == NULL)
{
printf
(
"\nlog.txt화일이 손상되었습니다. 종료합니다."
);
exit
(0);
}
fprintf
(log_fp,
"| %02d/%02d | %02d:%02d | %-20s | %-20s | %-10d\n"
,s_month,s_day,s_hour,s_min,ori_file,ori_word,wordcnt);
fclose
(log_fp);
}
int
main()
{
FILE
*ori_fp, *ch_fp, *mod_fp, *log_fp,*ch_fp2;
int
wordcnt=0,ch_len;
long
int
maxfile2=0,maxfile=0;
char
*readtemp,*findpoint, *chpoint, *modpoint;
char
ori_file[50],ori_word[50],ch_word[50], yorn;
log_fp =
fopen
(
"log.txt"
,
"r"
);
if
(log_fp == NULL)
{
log_fp =
fopen
(
"log.txt"
,
"w+"
);
fprintf
(log_fp,
"------------------------------------------------------------------------------\n"
);
fprintf
(log_fp,
"\"Find and Replace\" Program statistics.\nThis file is generated during first program session. new session will be appended\n"
);
fprintf
(log_fp,
"------------------------------------------------------------------------------\n"
);
fprintf
(log_fp,
"| Date | Time | FileName | FindWhat | # SearchNum \n"
);
fprintf
(log_fp,
"------------------------------------------------------------------------------\n"
);
fclose
(log_fp);
}
fclose
(log_fp);
printf
(
"------------------------------------------------------------------------------\n"
);
printf
(
"\t\t\t\tFind and Replace\n"
);
printf
(
"------------------------------------------------------------------------------\n"
);
printf
(
"::: File Name : "
);
gets
(ori_file);
printf
(
"------------------------------------------------------------------------------\n"
);
ori_fp =
fopen
(ori_file,
"r"
);
if
(ori_fp == NULL)
{
printf
(
"\"%s\" FILE open ERROR\n입력하신 파일이 존재하지 않습니다.\n"
,ori_file);
exit
(0);
}
ch_fp =
fopen
(
"result.txt"
,
"w+"
);
if
(ch_fp == NULL)
{
printf
(
"\"Result.txt\" can't not open!\nResult.txt화일을 생성할 수 없습니다. \n"
);
exit
(0);
}
fseek
(ori_fp, 0, SEEK_END);
maxfile =
ftell
(ori_fp);
fseek
(ori_fp, 0, SEEK_SET);
maxfile2 =
ftell
(ori_fp);
if
(maxfile>32768)
printf
(
"파일이 너무큽니다.버그가 존재 할 가능성이 있습니다.\n"
);
readtemp = (
char
*)
malloc
(maxfile+1);
memset
(readtemp,0x00,maxfile+1);
fread
(readtemp, maxfile, 1, ori_fp);
readtemp[maxfile] = 0x00;
findpoint = chpoint = readtemp;
while
(1)
{
printf
(
"\n>> Find what : "
);
gets
(ori_word);
for
(wordcnt=0;
strstr
(findpoint, ori_word);wordcnt++)
{
findpoint =
strstr
(findpoint, ori_word);
findpoint +=
strlen
(ori_word);
}
findpoint = chpoint = readtemp;
if
(!wordcnt)
{
printf
(
"\"%s\" 찾는 내용이 없습니다.\n"
,ori_word);
logman(ori_file,ori_word,wordcnt);
}
else
{
printf
(
"* \"%s\" 총 %d 건이 검색되었습니다.\n>> Quick Replace (y or n) : "
,ori_word,wordcnt);
scanf
(
"%c"
,&yorn);
fflush
(stdin);
if
(yorn==
'y'
)
{
while
(
strlen
(ori_word)!=
strlen
(ch_word))
{
printf
(
"\n>> Replace with : "
);
gets
(ch_word);
if
(
strlen
(ori_word)!=
strlen
(ch_word))
printf
(
"찾는찾는 문자열과 바뀔 문자열의 글자수는 반드시 통일 하세요.\n"
);
}
ch_len=
strlen
(ch_word);
for
(wordcnt=0;
strstr
(findpoint, ori_word);wordcnt++)
{
chpoint =
strstr
(findpoint, ori_word);
fwrite
(findpoint, chpoint-findpoint, 1, ch_fp);
fwrite
(ch_word, ch_len, 1, ch_fp);
chpoint += ch_len;
findpoint = chpoint;
}
logman(ori_file,ori_word,wordcnt);
printf
(
"* \"%s\" -> \"%s\" 총 %d 건을 수정하였습니다.\n"
,ori_word,ch_word,wordcnt);
break
;
}
}
}
fclose
(ch_fp);
fclose
(ori_fp);
free
(readtemp);
ori_fp =
fopen
(
"result.txt"
,
"r"
);
if
(ori_fp == NULL)
{
printf
(
"\"Result.txt\" can't not open!\nResult.txt화일을 생성할 수 없습니다. \n"
);
exit
(0);
}
ch_fp =
fopen
(
"result2.txt"
,
"w+"
);
if
(ch_fp == NULL)
{
printf
(
"\"Result.txt\" can't not open!\nResult.txt화일을 생성할 수 없습니다. \n"
);
exit
(0);
}
readtemp = (
char
*)
malloc
(maxfile);
memset
(readtemp,0,maxfile);
fread
(readtemp, maxfile, 1, ori_fp);
fwrite
(readtemp, maxfile, 1, ch_fp);
fclose
(ch_fp);
fclose
(ori_fp);
free
(readtemp);
return
0;
}</
time
.h>