| 고급 프로그래밍 및 실험 10주차 레포트 - 제출용버젼 |
Tech - https://ohyung.net/381 (YMD: 07/05/10 10:03)
한번에 크게 잡지 않고 한줄씩 읽어서 찾고 바꾸는것입니다.

sample.txt 에 들어있는 한글이나 영문을 찾은뒤 자신이 원하는 단어로 바꿔서 result.txt화일에 저장하는것입니다.
또한 자신이 찾은것을 log.txt에 저장하는것도 포함됩니다.

즉 두목의 은혜는 하늘 같아서 --> 스승의 은혜는 하늘 같아서...
이런식으로 변경 됩니다.

소스는 네이년에서 참고 했습니다. 제가 짠것은 찐빠나서;;;

Code Type : c
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include

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, *log_fp;
    long int wordcnt=0;
    char ori_file[20],ori_word[20],ch_word[20],read_temp[5000], write_temp[5000], yorn[5];
    char *findit_temp,*find_point, *read_point, *write_point;

    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);
    }

    while(1)
    {
        // 수정할것 입력받기부분
        wordcnt=0;
        printf("\n>> Find what : ");
        gets(ori_word);

        //찾기
        fseek(ori_fp, 0, SEEK_SET); 
        while(1) 
        {
            fgets(read_temp, 5000, ori_fp);
            read_point = read_temp;
            while(*read_point)
            {
                find_point = strstr(read_point, ori_word);
                if(!find_point) break;
                *find_point = 0;
                read_point = find_point + strlen(ori_word);
                wordcnt++;
            }
            if(feof(ori_fp)) break; // 파일의 끝이면 while(1) 탈출
        }
        fseek(ori_fp, 0, SEEK_SET); // 파일의 처음으로 이동
    
        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);
            gets(yorn);
            if(yorn[0]=='y')
            {
                //바꿀거
                while(strlen(ori_word)!=strlen(ch_word))
                {
                    printf("\n>> Replace with : ");
                    gets(ch_word);
                    if(strlen(ori_word)!=strlen(ch_word))
                        printf("찾는찾는 문자열과 바뀔 문자열의 글자수는 반드시 통일 하세요.\n");
                    
                }

                // 입력받은것을 바꾸는 부분
                while(1) 
                {
                    fgets(read_temp, 5000, ori_fp);
                    read_point = read_temp; 
                    write_point = write_temp;
                    while(*read_point)  
                    {
                        find_point = strstr(read_point, ori_word);
                        if(!find_point) // 없다면
                        {
                            strcpy(write_point, read_point);
                            fputs(write_temp,ch_fp);
                            break;
                        }
                        *find_point = 0; 
                        strcpy(write_point, read_point);
                        write_point += (find_point - read_point);
                        strcpy(write_point, ch_word); 
                        read_point = find_point + strlen(ori_word);
                        write_point += strlen(ch_word);
                    }
                    if(feof(ori_fp)) break;
                }

                logman(ori_file,ori_word,wordcnt);
                printf("* \"%s\" -> \"%s\" 총 %d 건을 수정하였습니다.\n",ori_word,ch_word,wordcnt);
                break;
            }
        }
    
    }
    fclose(ch_fp);
    fclose(ori_fp);
    
    return 0;
}
| 이 포스트에 대한 이용규약 |
Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시 2.0 라이센스 에 따라 이용하실 수 있습니다.
This work is licensed under a Creative Commons Attribution 2.0 Korea LicenseLink in a new window.

| 이 글과 태그로 연관된 글 |

| 트랙백 |
트랙백 주소 :: https://www.ohyung.net/rserver.php?mode=tb&sl=381