home > working > C/C++ > Source, Tip

 


Name  
   조규남 
Subject  
   파일에 존재하는 단어수 세기
subject: c, file reading, word count

  strtok 를 사용해서 ' ' 로 구별된 단어의 수를 계산합니다.

  사용방법은 도스창 에서 workdcount file1, file2, file3, .... 이렇게 입력하면

  입력된 모든 파일의 단어 숫자를 계산합니다.

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

#define MAXWORDSIZE     126
#define MAXWORDCOUNT    100

typedef char string[MAXWORDSIZE];

string g_word[MAXWORDCOUNT];
int g_wordcount = 0;
int g_wordsize[MAXWORDCOUNT];

void print_word_in_file(char *pszfilename);
void print_word_count(char *pszfilename);
int get_word_index(char *pszword);

main(int argc, char *argv[])
{
        int             i;
        
        for( i = 1 ; i < argc ; i ++ )
        {
                print_word_in_file(argv[i]);

                if( i != argc -1 )
                {
                        printf("Press any key to Next File\n");
                        _getch();
                }
        }
        printf("Press any key to quit...\n");
        _getch();
}

void print_word_in_file(char *pszfilename)
{
        FILE                *in;
        char                szBuffer[1024];
        char                seps[] = " \t\r\n";
        char                *token;
        
        g_wordcount = 0;
        memset(g_wordsize, 0, MAXWORDCOUNT * sizeof(int));
        
        if( (in = fopen(pszfilename, "rt")) != NULL )
        {
                while( fgets(szBuffer, 1024, in) != NULL )
                {
                        token = strtok(szBuffer, seps);

                        while( token != NULL )
                        {
                                int                index = get_word_index(token);
                                if( index == -1 )
                                {
                                        g_wordsize[g_wordcount] = 1;
                                        strcpy( g_word[g_wordcount++], token );
                                }
                                else
                                {
                                        g_wordsize[index]++;
                                }
                                token = strtok(NULL, seps);
                        }
                }
                fclose(in);
                print_word_count(pszfilename);
        }
}

void print_word_count(char *pszfilename)
{
        int                i;
        
        printf("\n *- %s -*\n", pszfilename);
        for( i = 0 ; i < g_wordcount ; i ++ )
        {
                printf("WORD: %s -> count: %d\n", g_word[i], g_wordsize[i]);
        }
        printf("\n");
}

int get_word_index(char *pszword)
{
        int                        index;

        for( index = 0 ; index < g_wordcount ; index ++ )
        {
                if( !strcmp(pszword, g_word[index]) )
                {
                        return index;
                }
        }

        return -1;
}

kvijmoy :: WuXOVGBJL 2009/09/26  

Name Memo Password  
        


Prev
   회원들의 소스를 올리는 곳입니다.

관리자
Next
   Linux 에서 Timer 사용 하는 방법

조규남


Copyright 1999-2018 Zeroboard / skin by JiYoo / edit by Mystous