| 고급 프로그래밍 및 실험 10주차 레포트 - 에러난 버젼 |
Technology 07/05/07 19:31 ( Translate this page  )
글 주소 : http://ohyung.net/380
기본적인 알고리즘 ( 찾기 수정제외 ) 을 보길 바랍니다.
이거 레폿용 SAMPLE.TXT 잘돌아갑니다...
하지만 파일이 커질경우 한 30k 정도 이상일 경우 결과 파일의 뒷부분에 쓰레기 값이 추가 됩니다....
그래서 버렸습니다.
조교님도 가지고 있는 소스니 그냥 내면 바로 감점 처리 될겁니다.
저는 바꿔서 제출하였기때문에;;; 이번엔 괜찮겠죠 ;;;;

네이년에 보면 누군가가 질문올렸더군요.. ( 검색어 : 두목의 은혜 )
그걸로 해결 하였습니다. -.-;

log.txt 파일 작성방법은 대충 아 이렇구나 하심 될겁니다.

그럼 건승을 기원하면서 내일 예비군 훈련장에서 보아요;;;;;;;

소스 코드 타입 : c
#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);
        //printf("| %2d/%2d | %2d:%2d | %-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;
       
        //memcpy(buf2,readtemp,maxfile+1);

        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; //findpoint를 맞추고
                                }
                                //나머지 부분
                                //fwrite(findpoint, maxfile-(strlen(readtemp)-strlen(chpoint)), 1, ch_fp);
                                //fwrite(findpoint, strlen(findpoint), 1, ch_fp);
                               
                                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;
}
태그 : (올블로그 tag 검색이올린 tag 검색Technorati tag검색) , (올블로그 tag 검색이올린 tag 검색Technorati tag검색) , (올블로그 tag 검색이올린 tag 검색Technorati tag검색) , (올블로그 tag 검색이올린 tag 검색Technorati tag검색) , (올블로그 tag 검색이올린 tag 검색Technorati tag검색)
| 이 포스트에 대한 이용규약 |
Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시 2.0 라이센스 에 따라 이용하실 수 있습니다.
This work is licensed under a Creative Commons Attribution 2.0 Korea LicenseLink in a new window.

| 트랙백 & 댓글|
트랙백 주소 :: http://ohyung.net/tb/380
댓글을 씁니다. ( Write a comment )