2008. 12. 15. 11:46
#include <stdio.h>
#include <malloc.h>

typedef struct { int key; } element;
typedef struct tree_node* tree_pointer;
enum {FALSE, TRUE};
struct tree_node {
        tree_pointer left_child;
        element data;
        short int bf;
        tree_pointer right_child;
};

struct stack {
        tree_pointer data;
        struct stack* node;
};

struct stack* top =NULL;
int unbalanced = FALSE;
tree_pointer root = NULL;

int x = 40 , y = 1;                          // X , Y 좌표

void avl_insert(tree_pointer* , element, int*);
void left_rotation(tree_pointer* ,int* );
void right_rotation(tree_pointer* ,int* );
void push(tree_pointer );
tree_pointer pop();
bool isempty();
void avl_tree_print();

void main()
{
        element data;
        for(int i = 0 ; i< 12; i++)
        {
                scanf("%d",&data.key);
                avl_insert(&root,data,&unbalanced);
        }
        avl_tree_print();
        

}

void avl_insert(tree_pointer* parent, element x, int* unbalanced)
{
        if(!*parent)
        {
                *unbalanced = TRUE;
                *parent = (tree_pointer)malloc(sizeof(tree_node));
                
                (*parent)->left_child = (*parent)->right_child = NULL;
                (*parent)->data = x;
                (*parent)->bf = 0;
        }
        else if(x.key < (*parent)->data.key)
        {
                avl_insert(&(*parent)->left_child,x,unbalanced);
                if(*unbalanced)
                {
                        switch((*parent)->bf)
                        {
                        case -1:
                                (*parent)->bf = 0;
                                break;
                        case 0:
                                (*parent)->bf = 1;
                                break;
                        case 1:
                                left_rotation(parent,unbalanced);
                                
                        }
                }
        }
        else if(x.key > (*parent)->data.key)
        {
                avl_insert(&(*parent)->right_child,x,unbalanced);
                if(*unbalanced)
                {
                        switch((*parent)->bf)
                        {
                        case 1:
                                (*parent)->bf = 0;
                                break;
                        case 0:
                                (*parent)->bf = -1;
                                break;
                        case -1:
                                right_rotation(parent,unbalanced);
                        }
                }
        }
        else
        {
                *unbalanced = FALSE;
                printf("The key is already in the tree");
        }
}

void left_rotation(tree_pointer* parent,int* unbalanced)
{
        tree_pointer grand_child, child;
        child = (*parent)->left_child;
        if(child->bf == 1)                  // LL 회전
        {
                (*parent)->right_child = child->right_child;
                child->right_child = *parent;
                (*parent)->bf = 0;
                *parent = child;                
        }
        else                               // LR 회전
        {
                grand_child = child->right_child;
                child->right_child = grand_child->left_child;
                grand_child->left_child = child;
                (*parent)->left_child = grand_child->right_child;
                grand_child->right_child = *parent;
                switch(grand_child->bf)
                {
                case 1:
                        (*parent)->bf = -1;
                        child->bf = 0;
                        break;
                case 0:
                        (*parent)->bf = child->bf = 0;
                        break;
                case -1:
                        (*parent)->bf = 0;
                        child->bf = 1;
                }
                *parent = grand_child;
        }
        (*parent)->bf = 0;
        *unbalanced = FALSE;
}




void right_rotation(tree_pointer* parent ,int* unbalanced)
{
        tree_pointer grand_child, child;
        child = (*parent)->right_child;
        if((*parent)->bf == -1 )             // RR 회전
        {
                (*parent)->right_child = child->left_child;
                child->left_child = *parent;
                (*parent)->bf = 0;
                (*parent) = child;
        }
        else                                 // RL회전
        {
                grand_child = child->left_child;
                child->left_child = grand_child->right_child;
                grand_child->right_child = child;
                (*parent)->right_child = grand_child->left_child;
                grand_child->left_child = (*parent);
                switch(grand_child->bf)
                {
                case -1:
                        (*parent)->bf = 1;
                        child->bf = 0;
                        break;
                case 0:
                        (*parent)->bf = child->bf = 0;
                        break;
                case 1:
                        (*parent)->bf = 0;
                        child->bf = -1;
                }
                (*parent) = grand_child;
        }
        (*parent)->bf = 0;
        *unbalanced = FALSE;
}

void push(tree_pointer data)
{
        if(top == NULL)
        {
                top = (struct stack*)malloc(sizeof(struct stack));
                top->data = data;
                top->node = NULL;
        }
        else
        {
                struct stack* new_node;
                new_node = (struct stack*)malloc(sizeof(struct stack));
                new_node->data = data;
                new_node->node = top;
                top = new_node;
        }
}

tree_pointer pop()
{
                tree_pointer data;
                struct stack* del_node;
                data = top->data;
                del_node = top;
                top = top->node;
                delete del_node;

                return data;
}
                
bool isempty()
{
        if(top == NULL)
                return TRUE;
        else
                return FALSE;
}

void avl_tree_print()
{
        tree_pointer current_node;
        current_node = root;
        bool done = TRUE;

        do
        {
                while(current_node != NULL)
                {
                        push(current_node);
                        current_node =current_node->left_child;
                        x-= 5;                // X 좌표 감소 좌측으로 이동
                        y+= 1;                // Y 좌표 증가 아래로 이동
                }

                if(top != NULL)
                {
                        current_node = pop();
                        y--;                  // Y 좌표 감소 위쪽으로 이동
                        
                        printf("%d",current_node->data.key);
                        current_node = current_node->right_child;

                        if(current_node != NULL)
                        {
                                x+=5;
                                y+=1;
                        }
                }
                else
                        done = FALSE;
        } while(done);

}
Posted by 두장
static,virtual,friend

C++ 문법의 마지막 부분으로 static,virtual,프렌드에 대해서 설명합니다. static의 개념은 클래스에 내장된 함수를 단지 그함수만 사용할수 있도록 보장을 하는 것이며 가상함수는 상속받은 함수에 오버라이딩을 하는것이아니라 두 개의 함수를 분리하여 설정하는 의미입니다. 프렌드의 개념은 성격이 다른 두 개의 클래스를 함께 사용하는 형태를 의미합니다.


1.static

static라는 선언은 클래스내에 있는 함수를 바로 외부에서 호출할수 있도록 하는형입니다. 다음은 static를 사용한 예입니다.


class CString{

       protected:

               char buff[80];

       public:

               static int GetIntValue(char *data);

};


int CString::GetIntValue(char *data)

{

       return atoi(data);

}


GetIntValue는 data로 넘겨준 인자의 값을 정수로 전환하여 리턴하는 함수입니다. 실제 이함수는 CString안에서도 사용할수 있지만 독자적으로 사용하여도 매우 편리한 함수입니다. 이럴 경우 외부에서 사용하고자 할 경우 형 앞에 static를 설정합니다. 이럴 경우 외부에서 바로 GetIntValue를 사용할수 있습니다.

다음은 위의 CString GetIntValue를 사용한 예입니다.



void main()

{

       char temp[80];

       int gab;

       sprintf(temp,"12345");

       gab=CString::GetIntValue(temp);

       printf("%d",gab);

       gets(temp);

}

위의 main함수에서 temp에 설정한 12345를 바로 정수값으로 넘겨 받기위해서 CString::GetIntValue함수를 그대로 호출하고 있습니다.

클래스 내에서 설정한 함수들중 외부에서 사용하여도 유용한 함수들일 경우 이형을 선언합니다.  static형은 virtual과 함께 하여 MFC에서 매우 많이 사용하는 형입니다. 이이유는 특정 클래스에서 특정함수들은 바로 호출을 하는 것이 편리할경우가 많기 때문입니다. 다음편에서 static의 사용례를 많이 보시게 될것입니다.




2.virtual

virtual 는 상위클래스에서 사용하는함수와 자신이의 함수를 동시에 사용하기 위해서 설정하는 형입니다. 오버로딩 예제인 CpEx07에서 Draw를 CFrameWnd에서 오버로딩했기 때문에 상위 클래스인 CWnd의 Draw를 사용하지 못합니다. 상위 클래스이 Draw도 사용하고 자신의 클래스인 Draw도 사용하고자 한다면 virtual이라는 형을 함수앞에 설정하면 됩니다.

다음은 virtual를 사용한 예입니다.


class CWnd{

       :

       void Draw();

       :

};

class CFrameWnd:public CWnd

{

       :

       virtual void Draw();

       :

};

void CFrameWnd::Draw()

{

       printf(프레임 윈도우 출력\n");

       CWnd::Draw();//상속받은 CWnd의 Draw함수를 호출한다.

}


위의 예에서 보면 CFrameWnd 안에 Draw를 오버로딩하면서 virtual의 형을 설정하였습니다. 이렇게 virtual 형을 설정하게 되면 상위 클래스인 CWnd의 Draw또한 사용할수 있습니다. CFrameWnd::Draw()의 내용을 보면 먼저 printf로 자신의 윈도우 정보를 출력하고 상속받은 상위 클래스의 CWnd::Draw()함수를 호출하였습니다. 이렇게 호출을 하면 CWnd의 Draw가 CFrameWnd의 맴버 변수의 값을 이용하여 가동하게 됩니다.

MFC의 프로그램에서는 virtual 함수를 매우 많이 사용하고 있습니다. 보통 윈도우에서 클래스의 맴버 함수들은 메시지 처리를 수행하는 부분이 많습니다. 이부분에서 메시지에 따라 특정 부분을 수행하고 나머지 복잡한 부분을 상위 클래스로 넘기는경우가 많습니다. MFC프로그램을 배울 때 이부분에 대해서 많은 것을 배우시게 될것입니다.

CpEx09는 CpEx07에서 Draw함수를 virtual로 사용한 예입니다.

(프로그램 소스)


//CWnd.h

//Cwnd 와 CFrameWnd클래스 헤더

class CWnd{

protected:

       int x;

       int y;

       int cx;

       int cy;

       int color;

public:

       void SetColor(int c);

       void SetPos(int x1,int y1,int x2,int y2);

       void Draw();

};


class CFrameWnd:public CWnd

{

protected:

       char manu[80];

public:

       void SetMenu(char *data);

       void PrintMenu();

       virtual void Draw();

};

/////////////////////////////////////////////////////

//CWnd.cpp

#include <stdio.h>

#include <string.h>

#include "CWnd.h"



void CWnd::Draw()

{

       printf("%d %d %d %d 에 %d 색으로 윈도우 그림\n",x,y,cx,cy,color);

}


void CWnd::SetPos(int x1, int y1, int x2, int y2)

{

x=x1;

y=y1;

cx=x2;

cy=y2;

}


void CWnd::SetColor(int c)

{

       color=c;

}


void CFrameWnd::SetMenu(char *data)

{

       strcpy(manu,data);

}


void CFrameWnd::PrintMenu()

{

       printf("메뉴명:%s\n",manu);

}

void CFrameWnd::Draw()

{

       printf("%d %d %d %d 에 %d 색으로 프레임 윈도우 출력\n",x,y,cx,cy,color);

       CWnd::Draw();//상속받은 CWnd의 Draw함수를 호출한다.

}


///////////////////////////////////////////////////////////

#include <stdio.h>

#include "CWnd.h"


void main()

{

       char temp[80];

       int x,y,cx,cy,color;

       CFrameWnd *pWnd= new CFrameWnd;

       printf("프레임을 그릴 좌표:");

       gets(temp);

       sscanf(temp,"%d %d %d %d",&x,&y,&cx,&cy);

       pWnd->SetPos(x,y,cx,cy);

       printf("색번호:");

       gets(temp);

       sscanf(temp,"%d",&color);

       pWnd->SetColor(color);

       pWnd->Draw();

       pWnd->SetMenu("프레임 메뉴");

       pWnd->PrintMenu();

       delete pWnd;

}




3.friend

friend란 간단하게 말해서 친구라는 의미입니다. 본편 앞에서사람이 자동차를 친구로 삼아서 멀리 갈수 있다는 말을 했습니다. friend란 자신과 연관이 없는 다른 클래스의 맴버를 자신의 것처럼 사용할수 있는 형태를 이야기 합니다. 예를 들어서 CDouble라는 소수를 컨트롤 하는 클래스가 다음과 같다고 합시다.



class CDouble{

       protected:

               int value;

       public:

       static int DoubletoInt(double gab);

};


int CDouble::DoubletoInt(double gab)

{

       double data;

       data=gab/10.;

       data=data+0.5;

       return (int)data;

}


CDouble클래스 안에 있는 DoubletoInt라는 함수는 소수를 정수로 바꾸는 함수입니다. 이함수와 함게 CInt라는 클래스가 다음과 같이 있다면


class CInt{

       protected:

               int value;

       public:

               void SetValue(double data);

               void SetValue(int data);

                       :

               int GetValue(){ return value;}

};

value에 값을 저장할 때 double값이 설정하는 SetValue라는 함수가 있습니다. 이함수는 double의 데이터를 int로 전환한다음 (반올림 하여) value값에 저장하는 기능이 되어야 합니다. 이함수에서 CInt는 CDouble클래스의  DoubletoInt 기능을 가진 함수를 하나 만들어야 합니다. 이렇게 만들어 double값을 int로 전환하고 이것을 value에 저장할수 있기 때문입니다. 여기에서 friend의  기능을 이용하여 CDoubletoInt안에 있는 DoubletoInt를 자신의 클래스에 삽입할수 있습니다.

다음은 CInt클래스에 DoubletoInt 함수를 프렌드로 이용한 예입니다.


class CInt{

       protected:

               int value;

       public:

               friend int CDouble::DoubletoInt(double gab);

               void SetValue(double data);

               int GetValue(){ return value;}

};

friend를 사용하여 int CDouble::DoubletoInt함수를 자신의 맴버 함수로 설정한 것입니다. friend는 MFC에서는 많이 사용하지 않습니다. friend의 의미란 계층적이고 구조적인 형태와는 걸맞지 않은 것이기 때문입니다. 그러나 어떤 기능이라는 것은 이해하고 넘어갈 필요가 있기에 설명합니다. CpEx10은 friend를 사용한 예제입니다.


#include <stdio.h>


class CDouble{

       protected:

               int value;

       public:

       static int DoubletoInt(double gab);

};


class CInt{

       protected:

               int value;

       public:

               friend int CDouble::DoubletoInt(double gab);

               void SetValue(double data);

               int GetValue(){ return value;}

};


void CInt::SetValue(double data)

{

       value=CDouble::DoubletoInt(data);

}


int CDouble::DoubletoInt(double gab)

{

       return (int)gab;

}


void main()

{

       double data;

       CInt m_int;

       data=3.142;

       m_int.SetValue(data);

       printf("%d\n",m_int.GetValue());

}

Posted by 두장

Below is a quick tutorial on how to nest a child dialog inside another using MFC (useful when, say, implementing your own tab-strip-like control). Disclaimer: caveat emptor — this method may be flawed, but it works for me.

  1. Using the resource editor, create the parent dialog. As the child dialog will be a child window, be sure to set Control Parent to True.

  2. Optional: Create and insert a placeholder control of the desired dimensions for your nested child dialog. I typically use a CStatic with a border and caption for reference. Assign it a resource id that isn’t IDC_STATIC and generate a control member variable because you will need it later.

    Here is a picture of a sample parent dialog with a placeholder control:

    Nested dialogs parent dialog

    If you do not use a placeholder control, you will have to set the child window position manually.

  3. Using the resource editor, create a dialog to act as the child. Make sure that it is no larger than your placeholder control or it will be truncated (unless you handle resizing). Set Style to Child, Control to True, and Border to None. TheControl setting is particularly important for a smooth user experience —Raymond Chen describes what it does in What is the DS_CONTROL style for?

    Here is a picture of a sample child dialog:

    Nested dialogs child dialog
  4. Assuming that you named the placeholder control member variablem_staticPlaceholder, insert the following code snippet into the parent dialog’sOnInitDialog() function:

    // Get the static placeholder client rectangle, which we will use
    // to size the child dialog.
    CRect rct;
    m_staticPlaceholder.GetWindowRect(&rct); // In screen coordinates
    ScreenToClient(&rct);
    m_staticPlaceholder.ShowWindow(SW_HIDE);
    
    CDialog* pchild = new CChildDialog;
    // Call Create() explicitly to ensure the HWND is created.
    pchild->Create(CChildDialog::IDD, this);
    
    // Set the window position to be where the placeholder was.
    pchild->SetWindowPos
        (
        NULL,
        rct.left,
        rct.top,
        rct.Width(),
        rct.Height(),
        SWP_SHOWWINDOW
        );
    

    You may want to examine SetWindowPos()’s flags to see if there is a more appropriate combination of flags for your usage scenario.

Here is a picture of the result where the child dialog is properly nested inside the parent:

Nested dialogs combined dialog

Using this method, even keyboard navigation works correctly!


Posted by 두장

string.h 함수 정리 (형식/기능)

 

 

함수명

형식 / 기능

strlen()

unsigned strlen( const char *str )

str 문자열 길이를 반환

strcat()

char *strcat( char *str1, const char *str2 )

str1의 문자열에 str2 문자열을 연결

strncat()

char *strncat( char *str1, const char *str2, unsigned c )

str1의 문자열에 str2 문자열의 선두 c개의 문자를 연결

strcmp()

char *strcmp( char *str1, const char * str2 )

str1의 문자열과 str2 서로 부호 없는 비교,
str1>str2
이면 양수값 반환 / str1<str2 이면 음수값 반환 / str1=str2 이면 0을 반환

strncpm()

char *strncpm( char *str1, const char *str2, unsigned c )

str1 문자열의 선두 c개의 문자와 str2 문자열을 비교

strcpy()

char *strcpy( char *str1, const char *str2 )

str1의 문자열에 str2 문자열을 복사, str1의 값을 반환함.

strncpy()

char *strncpy( char *str1, const char *str2, unsigned c )

str1의 문자열에 str2 문자열의 선두 c개의 문자를 복사

strstr()

char *strstr( const char *str1, const char *str2 )

문자열 str2가 문자열 str1에서 처음 일치하는 위치를 알려준다. 만약 문자열이 발견 되면 str1에 있는 그 문 자열의 포인터를 반환한다. 그 외에는 NULL 포인터를 반환한다.

strerror()

char *strerror( int num )

오류번호 num을 받아 해당하는 오류 메시지를 반환

strpbrk()

char *strpbrk( char *str1, const char *str2 )

지정한 str1의 문자열에서 str2의 문자열에 포함된 어떤 문자를 찾고 위치 포인터를 반환

strrchr()

char *strrchr( const char *str, int c )

문자열 str에서 c가 마지막으로 나타나는 위치를 알려준다. c가 발견되면 c의 포인터 를 반환한다. 그 외에는 NULL포인터를 반환한다.

strcspn()

char *strcspn( char *str, const char *str2 )

str1의 문자열에서 str2의 문자열에 포함된 문자가 첫 번째 나타날 때 까지의 문자수

strspn()

char *strspn( char *str1, const char str2 )

str1의 문자열에서 str2의 문자열에 포함되지 않은 문자가 첫 번째 나타날 때까지의 문자수

strtok()

char *strtok( char *str1, const char *str2 )

일련의 strtok 호출은 문자열 str1을 문자열 str2가 가지고 있는 문자로 구분되는 '토큰'으로 나눈다. 첫 번째 호출은 첫 번째 인수인 str1을 사용하여 두 번째 호출부터는 NULL을 첫 번째 인수로 사용한다. 매 호출마다 현재 토큰의 포인터를 반환한다. 더 이상 토큰이 없으면 NULL을 반환한다 .

strupr()

char *strupr( char *str )

지정한 str의 문자열 중 소문자를 대문자로 변환

 

 

또 다른 글 =============================================================================================

 

stpcpy

원형 : char *stpcpy( char *dest, char *src );

헤더 : string.h

기능 : 문자열 src를 문자배열 dest로 복사한다. dest 의 길이가 src 보다 작은 경우 dest 뒤의 인접 데이터가 파괴된다.

리턴 : dest + strlen(src)의 번지를 리턴

 

 strcat

원형 : char *strcat( char *dest, const char *src );

헤더 : string.h

기능 : 문자열끼리 연결한다. dest의 널문자 위치에 src가 붙는다. dest의 길이가 ( dest 문자열 + src 문자열 ) 길이보다 작을 경우 인접 데이터가 파괴된다.

리턴 : dest의 번지가 리턴

 

 strchr

원형 : char *strchr( const *src, int c );

헤더 : string.h

기능 : 문자열 배열 src 내에 문자 c가 있는지 검사하고 있을 경우 문자 c가 있는 번지를 리턴. 널문자도 문자로 취급하기 때문에 널문자를 넣으면 첫번째 널문자의 번지를 리턴한다.

리턴 : 문자열 src내에서 발견된 문자 c의 포인터. 발견되지 않을 경우 NULL 리턴

 

 strcmp

원형 : int strcmp( const char *src1, const char *src2 );

헤더 : string.h

기능 : 두 개의 문자열을 대소 비교함. 이 함수를 사용하여 두 개의 문자열이 같은 문자열인지 아닌지를 비교할 수 있다. 같을 경우 0, 다를 경우 양수나 음수의 값을 리턴.

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strcmpi

원형 : int strcmpi( const char *src1, const char *src2 );

헤더 : string.h

기능 : 두 개의 문자열을 대소 비교함. 하지만 대소문자를 구분하지 않는다. 이 함수를 사용하여 두 개의 문자열이 같은 문자열인지 아닌지를 비교할 수 있다. 같을 경우 0, 다를 경우 양수나 음수의 값을 리턴.

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strcoll

원형 : int strcoll( char *src1, char *src2 );

헤더 : string.h

기능 : setlocale 함수에 의해 지정된 비교방식에 따라 두 개의 문자열을 비교

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strcpy

원형 : char *strcpy( char *dest, const char *src );

헤더 : string.h

기능 : 문자열 src를 문자배열 dest로 복사한다. dest 의 길이가 src 보다 작은 경우 dest 뒤의 인접 데이터가 파괴된다.

리턴 : dest의 번지가 리턴

 

 strcspn

원형 : size_t strcspn( const char *src1, const char *src2 );

헤더 : string.h

기능 : 문자열 src1 중에 문자열 src2에 들어 있지 않은 문자들이 연속해 있는 길이를 구한다. 문자열 s2에 없는 문자들로 구성되어 있는 문자열 s1 내의 세그먼트를 찾아내고 그 세그먼트의 길이를 리턴.

예를 들어 src1 = "123456" 이고 src2 = "0486" 이라면 src1 내에서 src2 의 문자인 4가 나오기 전까지의 길이인 3을 리턴한다.

리턴 : 찾아낸 세그먼트의 길이만약 0을 리턴한다면 세그먼트가 발견되지 않은 것이다.

 

 strdup

원형 : char *strdup( const char *src );

헤더 : string.h

기능 : 새로운 메모리를 할당하고 문자열 src를 복사한 후 그 포인터를 리턴한다. 종료 시 반드시 복제된 문자열은 메모리를 해제해 주어야 한다.

리턴 : 복제된 문자열의 포인터를 리턴. 메모리 할당에 실패한 경우 NULL을 리턴.

 

 _strerror

원형 : char *_strerror( const char *src );

헤더 : string.h, stdio.h

기능 : 사용자가 정의하는 에러 메시지를 만든다. src NULL일 경우 최근 발생한 에러 메시지만으로 구성된 문자열을 만들고 src NULL이 아닌 경우 에러메시지 형식의 문자열을 만든다.

만약 src "babo" 이면 babo : 최근 에러 메시지 같은 형식으로 문자열을 만든다.

리턴 : 만들어진 에러 문자열의 포인터를 리턴한다.

 

 strerror

원형 : char *strerror( int errnum );

헤더 : stdio.h

기능 : errnum에 해당하는 에러메시지 문자열을 만든다. 에러메시지 문자열은 모두 \n으로 끝나므로 개행 문자를 첨가해 줄 필요는 없다.( 에러 메시지는 이미 정의되어 있으므로 궁금하면 출력시켜보자)

리턴 : 에러메시지 문자열의 포인터

 

 stricmp

원형 : int stricmp( const char *src1, const char *src2 );

헤더 : string.h

기능 : 두 개의 문자열을 대소 비교함. 하지만 대소문자를 구분하지 않는다. 이 함수를 사용하여 두 개의 문자열이 같은 문자열인지 아닌지를 비교할 수 있다. 같을 경우 0, 다를 경우 양수나 음수의 값을 리턴이 함수를 매크로로 정의해 둔 함수가 strcmpi 이다.

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strlen

원형 : size_t strlen( const char *src );

헤더 : string.h

기능 : 문자열 src 의 길이를 계산한다. 널문자는 길이에 포함되지 않고 널문자 앞에 까지 문자 개수를 리턴한다.

리턴 : 문자열의 길이

 

 strlwr

원형 : char *strlwr(char *src);

헤더 : string.h

기능 : 문자열 src 내의 대문자(A~Z)를 소문자(a~z)로 변경한다. 영문자 외의 다른 문자는 변경하지 않는다.

리턴 : 인수로 주어진 src를 그대로 리턴

 

 strncat

원형 : char *strncat( char *dest, const char *src, size_t maxlen );

헤더 : string.h

기능 : dest 의 널문자에 src의 문자열을 붙이는데 maxlen 길이 만큼만 붙인다.

리턴 : dest의 번지를 리턴

 

 strncmp

원형 : int strncmp( const char *src1, const char *src2, size_t maxlen );

헤더 : string.h

기능 : 두 개의 문자열을 대소 비교한다. 비교시 대소문자를 구분한다. 하지만 문자열 전체를 비교하는 것이 아니라 maxlen 길이 만큼만 비교한다.

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strncmpi

원형 : int strncmpi( const char *src1, const char *src2, size_t maxlen );

헤더 : string.h

기능 : 두 개의 문자열을 대소 비교한다. 비교시 대소문자를 구분하지 않는다하지만 문자열 전체를 비교하는 것이 아니라 maxlen 길이 만큼만 비교한다.

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strncpy

원형 : char *strncpy( char *dest, char *src, size_t maxlen );

헤더 : string.h

기능 : 문자열을 정해진 길이만큼 복사한다. 길이만큼만 복사하기 때문에 dest의 뒤에 널문자가 붙지 않는다.

리턴 : dest의 번지를 리턴

 

 strnicmp

원형 : int strnicmp( const char *src1, const char *src2, size_t maxlen );

헤더 : string.h

기능 : 두 개의 문자열을 대소 비교한다. 비교시 대소문자를 구분하지 않는다하지만 문자열 전체를 비교하는 것이 아니라 maxlen 길이 만큼만 비교한다. 이 함수의 매크로 버젼이 strncmpi 이다.

리턴 : src1 < src2  인 경우 음수 리턴

         src1 = src2  인 경우 0 리턴

         src1 > src2  인 경우 양수 리턴

 

 strset

원형 : char *strset( char *src, int c );

헤더 : string.h

기능 : 문자열을 특정 문자로 채운다. 문자열 중 널문자가 발견될 때가지 문자 c 로 채운다.

리턴 : src의 번지를 리턴

 

 strpbrk

원형 : char *strpbrk( const char *src1, const char *src2 );

헤더 : string.h

기능 : 문자열 src1 내에 문자열 src2에 있는 문자 중의 하나라도 있는지 검사하고 있을 경우 그 포인터를 리턴.

리턴 : 문자열 src1 내에 문자열 src2에 있는 문자 중 하나가 발견된 포인터를 리턴. 없을 경우 널을 리턴

 

 strrchr

원형 : char *strrchr( const cjar *src, int c );

헤더 : string.h

기능 : 문자 배열 src내에 문자 c가 있는지 검사하고 있을 경우 문자 c가 있는 번지를 리턴한다. 하지만 앞에서 부터 검사하는 것이 아니라 뒤에서 부터 검사를 진행한다.

리턴 : 문자열 src에서 발견된 문자 c의 포인터를 리턴. 발견되지 않을 경우 널을 리턴.

 

 strrev

원형 : char *strrev( char *src );

헤더 : string.h

기능 : 문자열의 순서를 바꾼다. 즉 제일 앞에 있는 문자가 제일 뒤로 가고 제일 뒤의 문자가 제일 처음으로 옮겨진다. 널문자는 이동 없음.

리턴 : 역순으로 바꾼 문자열의 src번지

 

 strspn

원형 : size_t strspn( const char *src1, const char *src2 );

헤더 : string.h

기능 : 문자열 src1 중에서 문자열 src2에 있는 문자들이 연속해 있는 길이를 구한다.

예를 들어 src1 = "12345678" 이고 src2 = "143290" 이라면 src1에서 src2의 문자들을 찾아보면 1234가 있다. 이 문자열의 길이 4를 리턴한다.

리턴 : 찾아낸 세그먼트의 길이를 리턴. 만약 0을 리턴하면 세그먼트를 찾지 못한 것이다.

 

 strstr

원형 : char *strstr( const char *src1, const char *src2 );

헤더 : string.h

기능 : 문자열 src1내에서 문자열 src2가 포함되어 있는지 검사하고 있다면 src2와 같은 문자열이 있는 부분을 리턴한다.

리턴 : 부분문자열의 선두 번지. 없다면 널을 리턴

 

 strtod

원형 : double strtod( const char *src1, char **endptr );

헤더 : stdlib.h

기능 : 문자열을 double 형 부동 소수점으로 바꾼다. 문자열은 다음과 같은 구조로 이루어져야 한다.

[공백,][부호][숫자][소수점][e]  -> ex) -5.64e+5, -3.6924

순서를 어기거나 불필요한 문자가 중간에 있을 경우는 변환이 중지.

endptr 인수는 변환이 중지될 경우 중지된 문자를 가리키므로 에러 검색에 사용

리턴 : 변환된 double 형 실수를 리턴. overflow 발생시 HUGE_VAL을 리턴

 

 strtok

원형 : char *strtok( char *src1, char *src2 );

헤더 : string.h

기능 : 문자열에서 token을 찾아낸다. ( token 이란 특정한 구분자로 분리되는 문장 구성 요소이다. 구분자가

"/"라 할때 다음 문자열은 1994 Feb 20의 세 개의 token 으로 분리된다. "1994/Feb/20" )

문자열 src1 token을 찾아낼 문자열이며 src2 token 구분자가 들어있는 문자열이다. token 구분자는 여러개가 복수 개 지정될 수 있으며 token 구분자는 strtok 호출 시 마다 바뀔 수 있다.

리턴 : 찾아낸 token의 포인터를 리턴. token이 더 이상 없을 경우 널 리턴.

( 이해가 안가는 분은 winapi에서 예제를 참고)

 

strtol

원형 : long strtol( const char *src1, char **endptr, int radix );

헤더 : stdlib.h

기능 : 문자열을 long 형 정수로 변환한다.문자열 src1은 다음과 같은 구조로 이루어져야 한다.

[공백, ][부호][숫자]

만약 이 순서를 어기거나 불필요한 문자가 문자열 내에 있을 경우 변환은 중지되고 중지된 위치가 endptr에 설정된다. endptr은 에러 검색에 사용하면 용이. 그리고 radix은 문자열을 해석할 진법을 지정할 때 사용되는데 범위는 2~36 까지 이고 그 진법에서 사용되는 문자만 인식

리턴 : 변환된 long 형 정수를 리턴. 변환이 불가능할 경우 0을 리턴

 

strtoul

원형 : unsigned long strtoul(const char *src1, char **endptr, int radix );

헤더 : string.h

기능 : 문자열을 부호없는 long 형 정수로 바꾼다. 사용법은 strtol 과 동일.

리턴 : 변환된 unsigned long 형 정수를 리턴. 변환이 불가능할 경우 0을 리턴

 

strupr

원형 : char *strupr( char *src );

헤더 : string.h

기능 : 문자열 src내의 소문자 (a~z)를 대문자( A~Z)로 변환한다. 영문자 외의 다른 문자는 변경하지 않는다.

리턴 : src의 번지를 리턴

출처&참조 : www.winapi.co.kr

 

Posted by 두장
에디트박스와 리스트 컨트롤의 내용이 꽉 찰경우에 자동으로 스크롤을 내려줄 때
다음과 같이 구현합니다.

1. EditBox Control
CEdit m_edit;
m_edit.LineScroll(m_edit.GetLineCount());
2. List Control
CListCtrl m_list;
m_list.EnsureVisible(m_list.GetItemCount()-1, FALSE);

Posted by 두장
2008. 10. 31. 16:03

 

11.1 표준 입·출력 함수

표준 입·출력 함수는 자료의 기록, 판독에 대하여 다음과 같이 4가지 방법을 사용한다.

① 키보드로부터 자료를 읽어서 화면에 자료를 나타내는 방법으로 한 번에 한문자만을기록, 판독하는 방법

(예: getchar(), getc(), getch(),putchar(), putc(), putch())

② 키보드에서 문자열을 읽어서 화면에 문자열을 나타내주는 방법

(예: gets(), puts())

③ 문자, 문자열, 부동 소수점, 정수 등을 혼합하여 형식화된 자료를 기록하거나 판독하는 방법

(예: fscanf(), fprintf())

④ 레코드 또는 블록 형식을 이용하여 자료의 기록 및 판독을 행하는 방법 등이 있다.이중 2가지 방법은 이미 배운바 있으나 정리하면서     간략히 설명하고 3,4번째 방법에대해 자세히 기술한다.

단일 문자 입·출력

■ 입 력

키보드로부터 1문자를 입력하는 형식은 다음과 같다.

 

getchar()

getch()

getc(화일 포인터)

 

getchar(), getch(), getc() 함수의 사용 방법은 동일하나 getch() 함수는 화면에 문자가 나타나지 않으며 getc() 함수는 인수로서 읽고 쓸 화일의 이름을 꼭 밝혀 주어야 한다.

■ 출 력

1 문자를 화면에 출력하는 형식은 다음과 같다.

 

putchar()

putch()

putc(문자, 화일 포인터)

 

putchar(), putch(), putc() 함수의 사용 방법은 동일하나, putchar(), putch() 함수는 화면에서 출력만으로 사용하며 putc() 함수는 화일 포인터에 문자를 저장할 수 있다.

 

 [예제 11-1] putchar() 함수 사용 방법

 #include <stdio.h>

void main()

     {

         char ch;

         while((ch = getchar()) != '\r')

         putchar(ch);

     }

 TurboC example

TurboC example

 

 

[예제 11-2] putch() 함수 사용 방법  

#include <stdio.h>

void main()

    {

        char ch;

        while((ch = getch()) != '\r')

        putch(ch);

    }

TurboC example 

 

 

[예제 11-3] getc(), putc() 함수 사용 방법  

 #include <stdio.h>

void main()

     {

          char ch;

          while((ch = getc(stdin)) != '\r')

          putc(ch,stdout);

     }

TurboC example

TurboC example 

 

 

문자열의 입·출력

문자열의 입·출력에 대한 형식은 다음과 같다.

 

gets(문자열)

puts(문자열)

 

gets() 함수는 표준 입력 장치(스트림 입력 장치라고도 함)으로부터 문자열을 입력으로 받아들여 문자열에 대한 포인터를 반환하는 함수이다 이때 주의할 점은 문자열 버퍼의 끝에 NULL('\0')이 자동적으로 부가되어 저장되므로 입력의 문자 수보다 적어도 하나 이상의 저장공간이 확보되어야 한다. puts() 함수는 표준 출력 장치로 문자열을 보내는 함수이다. 이 함수는 printf()의 제어지시를 이용하여 문자열을 간단히 출력할 수 있으므로 잘 사용하지 않는다.

 

[예제 11-4]  

#include <stdio.h>

void main()

     {

          char string[80];

          printf("Input a string:");

          gets(string);

          puts(string);

     }  

Input a string:This is sample string

This is sample string 

 

 

11.2 화일의 개방과 종결

본 절에서는 보조 기억장치를 이용한 화일의 기록, 판독에 대한 초기 과정, 화일의 개방과 종결에 대해 기술한다.

화일의 개방

화일은 보조 기억장치에서 읽어오거나 보조 기억장치로 기억시키기 위해서는 화일을 개방하여야 한다. 화일을 개방하기 위해서는 화일이름을 읽을 것인지 기억 시킬것인지를 운영체제에 정보를 제공해 주어야 한다. 운영 체제는 보조 기억장치(주로 디스크 장치 이용)에서 정보를 읽을 것인지 또는 기억할 것인지에 대한 정보를 포인터로써 돌려주게 된다. 따라서 개방되는 화일은 구조체를 가지게 된다.

화일 구조체는 자료 버퍼의 현재 크기와 위치 등의 화일에 관한 정보를 포함하며 반드시 #include <stdio.h> 할 필요가 있다.

프로그램에서 화일 구조체에 대한 포인터형의 변수 선언과 화일을 개방하는 문장은 다음과 같다.

예)#include <stdio.h>

FILE *fp;

fp=fopen("kim.dat","w");

여기서 *fp는 포인터 화일 변수이며 fopen() 함수는 fp변수에 저장된 화일 구조체에 대한 포인터를 돌려준다. 개방 화일은 "kim.dat"이며 화일에 자료를 기록하라는 의미가 "w"이다.

<stdio.h> 화일에 미리 정의되어 개방되는 스트림은 다음과 같다.

 

이 름

장 치

모 드

stdin

stdout

stderr

stdaux

stdprn

표준 입력 장치(키보드)

표준 출력 장치(화면)

표준 에러 장치(화면)

표준 보조 장치(직렬 포트)

표준 출력 장치(병렬 프린터)

텍스트 모드

텍스트 모드

텍스트 모드

2 진 모드

2 진 모드

 

FILE 구조체에 대한 포인터형의 변수와 화일의 개방함수 fopen()에 대해 생각해 보자. fopen() 함수의 목적은 입·출력 동작을 하기 위해 화일을 개방하는데 사용되며 형식은 다음과 같다.

 

FILE *fopen(화일명, 접근 모드)

 

여기서 접근 모드는 개방하고자 하는 화일의 모드를 결정하는 문자열을 말하며 다음과 같은 사용 모드를 제공하고 있다.

 

분 류

접근 모드

접근 내용

읽 기

r

읽기 전용으로 기존의 화일을 개방

쓰 기

w

a

쓰기 전용으로 새로운 화일을 생성

추가로 쓰기 위해 화일을 개방

갱 신

r+

w+

a+

갱신을 위해 기존의 화일을 개방

갱신을 위해 새로운 화일을 생성

갱신을 위해 화일의 끝에 개방

2진 모드

"rb", "wb", "ab"

"r+b" 또는 "rb+"

"r+a" 또는 "ra+"

2진 화일에 대해 r,w,a 기능 수행

2진 화일에 대해 r+ 기능 수행

2진 화일에 대해 a+ 기능 수행

 

화일 닫기 화일을 개방하여 그 화일에 대한 기록과 판독이 끝나면 그 화일을 닫아야만 한다. 화일을 닫는 형식은 다음과 같다.

 

fclose(화일 포인터)

11.3 화일 입·출력 함수

이 절에서는 화일의 기록과 판독에 따른 입·출력 형식에 대해 기술한다.

문자 단위 화일 입·출력 함수

일단 화일을 개방하면 디스크에 기록시키거나 디스크에서 화일의 내용을 읽어올 수가 있다. 문자 단위 화일 입·출력 형식은 다음과 같다.

 

fgetc(화일 포인터)

fputc(문자, 화일 포인터)

 

 

[예제 11-5] fputc() 함수를 이용하여 화일에 출력하는 프로그램  

 #include <stdio.h>

void main()

     {

          FILE *fp;

          char ch;

          fp=fopen("kim.dat","w");

          while((ch=getch()) != '\r') {

               putch(ch);

               fputc(ch,fp);

          }

          fclose(fp);

     }

"kim.dat"화일에는 키보드를 통해서 입력된 문자가 저장된다. 

 

 

[예제 11-6] fgetc() 함수를 이용하여 화일에 있는 내용을 화면에 출력하는 프로그램  

#include <stdio.h>

void main(void)

     {

          FILE *fp;

          char string[] = "This is a test";

          char ch;

          if((fp=fopen("kim.dat", "r")) == NULL) {

               printf("\n File not found");

               exit(-1);

          }

          do

          {

               ch = fgetc(fp);

               putch(ch);

          } while (ch != EOF);

          fclose(fp);

     }

"kim.dat" 화일에 있는 내용이 화면에 출력된다.

만약 "kim.dat"라는 화일이 없으면

"File not found" 라는 메시지가 출력된다.

  

 

  문자열의 화일 입·출력 함수

화일에서 문자열을 판독하거나 화일 문자열을 기록하는 형식은 다음과 같다.

 

fgets(문자열, 최대 길이, 화일 포인터)

fputs(문자열, 최대 길이 화일 포인터)

 

fgets() 함수는 화일 포인터로부터 입력행을 읽어 문자 배열에 저장하고 현재 위치로부터 한 레코드 또는 (최대 길이-1)개의 문자가 읽혀지면 개행문자를 만나면 끝낸다. 마지막 문자가 정상적으로 읽혀지면 NULL('\n')을 추가시키며 화일의 끝이거나 에러이면 NULL을 반환한다.

fputs() 함수는 화일 포인터가 지시하는 화일에 문자열을 기록하라는 것을 의미하며 정상이면 0을 화일의 끝이거나 에러이면 EOF(-1)을 반환한다.

 

[예제 11-7] fgets(), fputs() 함수 사용 방법  

#include <stdio.h>

void main()

     {

          FILE *fp,*fp1;

          char string[30];

          fp=fopen("kim1.dat","w");

          fp1=fopen("kim.dat","r");

          while(fgets(string,30,fp1) != NULL)

          fputs(string,fp);

          fclose(fp);

          fclose(fp1);

}

c:>type kim.dat

Jim Botton, sportscaster and author of Ball Four, on

the reception to his book : "Pete rose would screen at

me from dugout because I resealed too much. Now he's

posing in magazine ads in his underwear.

   

 

 

"kim1.dat" 화일에는 "kim.dat"화일에 있는 내용과 동일하다.

형식화된 화일 입·출력 함수

지금가지 문자와 텍스트를 화일에 기록하거나 판독하는 것을 다루었다. 여기서는 수치 자료를 다루기 위해서 형식화된 입·출력 함수에 대해 설명한다. 형식화된 입·출력 함수에 대한 다음과 같다.

 

fscanf(화일 포인터, 제어문자열, 인수)

fprintf(화일 포인터, 제어문자열, 인수)

 

fscanf() 함수fprinf() 함수는 화일 포인터를 제외하고는 scanf() 함수와 fprintf() 함수와 동일하다. fscanf() 함수는 화일로 부터 지정된 형태로 읽어들여 뒤에 오는 인수에 대입하고 화일의 끝이거나 에러가 발생하면 EOF를 반환하고 그렇지 않을 경우에는 입력된 항목의 수 를 반환한다.

fprintf() 함수는 지정된 형태를 화일에 출력하고 화일에 쓰여진 문자의 수를 반환하고 에러가 생길 경우에는 음수(-)의 값을 반환한다.

 

[예제 11-8] 성적을 저장하는 프로그램  

#include <stdio.h>

void main()

     {

          FILE *fp;

          char name[20];

          int number;

          int kor;

          int eng;

          int math;

          int total;

          int i=0;

          fp=fopen("grade.dat","w");

          do {

               printf("Enter name, number, Kor, Eng, Math : ");

               scanf("%s %d %d %d %d",name, &number, &kor, &eng,                          &math);

               total=kor+eng+math;

               fprintf(fp,"\n%s %d %d %d %d %d",name, number, kor, eng,

                         math, total);

               i++;

          } while(i <4);

          fclose(fp);

     }

Enter name, number, Kor, Eng, Math : kim 1 57 87 45

Enter name, number, Kor, Eng, Math : kim1 2 67 45 76

Enter name, number, Kor, Eng, Math : kim2 3 76 45 95

Enter name, number, Kor, Eng, Math : kim3 4 67 45 87

c:>type grade.dat

kim 1 57 87 45 189

kim1 2 67 45 76 188

kim2 3 76 45 95 216

kim3 4 67 45 87 199 

 

 

[예제 11-9] 성적이 있는 화일을 읽어서 화면에 출력하는 프로그램  

 #include <stdio.h>

void main()

     {

          FILE *fp;

          char filename[15];

          char name[20];

          int number;

          int kor;

          int eng;

          int math;

          int total;

          printf("Enter name of a file to open foe WRITING : ");

          gets(filename);

          if((fp=fopen(filename,"r")) == NULL) {

               printf("\n File not found");

               exit(-1);

          }

          printf("\n Name Number KOR ENG MATH TOTAL");

          do {

               fscanf(fp,"%s %d %d %d %d %d",name, &number, &kor,

                         &eng, &math, \&total);

               printf("\n %-10s%3d %3d %3d %3d %3d", name, number, kor,                          eng, math,total);

          } while(!feof(fp));

          fclose(fp);

     }

Enter name of a file to open foe WRITING : grade.dat

Name Number KOR ENG MATH TOTAL

kim 1 57 87 45 189

kim1 2 67 45 76 188

kim2 3 76 45 67 216

kim3 4 67 45 87 199 

 

 만일 "grade.dat"라는 화일이 없으면 "File not found"라는 메시지가 출력된다.

레코드 단위의 화일 입·출력 함수

형식화된 화일 입·출력 함수는 각 숫자가 문자로서 저장되기 때문에 디스크의 공간을 많이 차지하게 된다.

또한 배열이나 구조체 같은 복잡한 자료 형을 읽거나 기록할 때 직접적인 방법이 없다.

배열을 한 번에 배열 요소 하나를 저장하기 때문에 비 효율적이며 구조체는 여러 부분으로 나누어서 저장해야 한다.

이러한 문제점을 해결하는 것이 레코드 단위의 입·출력이다. 레코드 단위의 입·출력은 수치를 정수형은 2바이트,

실수형은(부동 소수점형)은 4바이트를 2진 형식으로 쓰기 때문에 메모리를 효율적으로 이용할 수가 있다.

한꺼번에 많은 자료를 취급할 수가 있으며 배열, 구조체, 배열의 구조체, 그리고 다른 자료 구조를 단일 문장으로 사용할 수가 있다.

형식은 다음과 같다.

 

fwrite(문자열, 크기, 항목수, 화일 포인터)

fread(문자열, 크기, 항목수, 화일 포인터)

 

fwrite() 함수는 지정된 갯수 만큼의 자료를 화일에 저장하며 fread() 함수는 주어진 화일 로부터 지정된 갯수만큼의 자료를 읽어서 문자열에 저장한다.

 

[예제 11-10] fwrite() 함수를 이용한 프로그램  

#include <stdio.h>

void main()

     {

          struct {

               char name[20];

               int number;

               float height;

          } man;

          FILE *fp;

          float dummy=0.0;

          if((fp=fopen("kim.dat","wb")) == NULL) {

               printf("\n File not found");

               exit(-1);

          }

          do {

               printf("\n Enter name : ");

               gets(man.name);

               printf(" Enter number : ");

               scanf("%d",&man.number);

               printf(" Enter height : ");

               scanf("%f",&man.height);

               fflush(stdin);

               fwrite(&man, sizeof(man),1,fp);

               printf("Add another man(y/n) ?");

          } while(getch() == 'y');

          fclose(fp);

     }

Enter name : kim1

Enter number : 1

Enter height : 34.83

Add another man(y/n) ?

Enter name : kim2

Enter number : 2

Enter height : 67.56

Add another man(y/n) ? y

Enter name : kim3

Enter number : 3

Enter height : 84.54

Add another man(y/n) ? n 

 

 

[예제 11-11] fread() 함수을 이용한프로그램  

#include <stdio.h>

#include <stdlib.h>

void main()

     {

          struct {

               char name[20];

               int number;

               float height;

          } man;

          FILE *fp;

          fp=fopen("kim.dat","rb");

          while(1) {

               fread(&man, sizeof(man), 1, fp);

               if(feof(fp)) {

                    fclose(fp);

                    exit(-1);

               }

               printf("\n Name : %s", man.name);

               printf("\n Numer : %d", man.number);

               printf("\n Height : %4.2f",man.height);

           };

     }

Name : kim1

Number : 1

height : 34.83

Name : kim2

Number :2

height : 67.56

Name : kim3

Number : 3

height : 84.54  

 

 여기서 feof() 함수는 화일의 끝을 검사하는 함수이며 화일 포인터가 가리키고 있는 화일이 모두다 판독되면 ZERO(0)가 아닌 값을 반환하고 그렇지 않으면 0을 반환한다.

exit(n) 함수는 프로그램의 실행중 중도에 끝내고 싶을 때 이용되는 함수이며 n은 0이 아니어야 프로그램의 수행을 종료하는 함수이다.

화일 포인터의 제어 함수

이제가지의 순차화일에 비해 다소 조작 방법이 복잡하지만 어디서든지 읽고 쓰기 가능한 방식은 "랜덤화일처리"라고 하며, 랜덤화일처리에서 어느 부분을 읽을 것인지를 지정할 수 있는데 이를 수행하는 함수가 fseek() 이다.

fseek() 함수는 화일의 위치를 자유로이 지정하는 함수로서 다음과 같은 형식을 갖는다.

 

fssek(화일 포인터, 변위, 기준 점)

 

화일 포인터의 화일 위치를 기준점을 기준으로 하여 변위 바이트만큼 이동하여 정상시는 0, 오류시는 0 이외의 값을 반환한다.

이때 기준점의 위치 설정에 따라 3가지로 나누며 이에 대한 상수 및 의미는 표 11-3과 같다.

 

상 수

실 제 값

의 미

SEEK_SET

SEEK_CUR

SEEK_END

0

1

2

화일의 처음

현재 화일 포인터의 위치

화일끝 검출 위치

 

한 편 fseek() 이외의 랜덤 처리계 함수 및 기능은 표 11-4와 같은 종류가 있다.

 

함수명

기 능

fflush

fgetpos

fsetpos

ftell

rewind

버퍼에 넣으려는 전체의 문자를 화일에 씀

화일 위치의 정보를 읽음

화일 포인터의 위치 설정

현제의 화일 위치 파악

화일의 위치를 선두로 이동

 

 

[예제 11-12] fseek() 함수를 사용한 프로그램  

#include <stdio.h>

#include <stdlib.h>

void main()

     {

          struct {

               char name[20];

               int number;

               float height;

          } man;

          FILE *fp;

          int rec_no;

          long int offset;

          fp=fopen("kim.dat","rb");

          printf("Enter record number : ");

          scanf("%d",&rec_no);

          offset=(rec_no-1) * sizeof(man);

          if(fseek(fp,offset,SEEK_SET)) {

               printf("\n Can't move pointer there.");

               exit(-1);

          }

          fread(&man, sizeof(man), 1, fp);

          printf("\n Name : %s", man.name);

          printf("\n Numer : %d", man.number);

          printf("\n height : %f",man.height);

          fclose(fp);

      }

Enter record number : 2

Name : kim2

Numer : 2

height : 67.55998 

 

 

11.4 메모리 할당

지금까지 다루어온 자료의 저장 방법은 표준 변수들을 사용하여 기억 장소를 미리 확보하여 확보된 영역에서의 자료의 저장 방법을 다루었다.

예를 들어 배열 선언에서 100개의 원소를 취하는 배열을 확보하고 5개의 원소만을 취한다면 95개의 원소는 빈 공간으로 남아 기억 공간이 낭비가 된다.

이 절에서는 자료의 크기가 동적으로 변할때 메모리의 확대가 가능한 메모리 할당 에 대하여 기술한다. c언어에서는 malloc() 함수를 제공하여 처리하고 있다. malloc() 함수의 사용 형식은 다음과 같다.

 

malloc(arg)

 

여기서 arg는 메모리의 바이트를 표시하는 양의 정수이며 새로운 메모리의 블럭 시작점에 대한 포인터를 반환한다.

 

[예제 11-12] malloc() 함수를 이용한 프로그램  

#include <string.h>

#include <stdio.h>

void main()

     {

          char temp_string;

          temp_string=(cahr *) malloc(100);

          strcpy(temp_string,"Hello Kang");

          printf("Enter string : ");

          fgets(temp_string,100,stdin);

          printf("Your input string is %s",temp_string);

     }

Hello Kang

Enter string : Good morning !

Your input string is Good morning !

 

 

다음 표 11.5는 표준 입·출력 함수에 대하여 총괄적인 의미를 보인 것이다.

 

함 수 명

형 식

의 미

getc(fp)

getc(fp)

FILE *fp;

fp가 가르키는 화일로부터 한 문자 입력

putc(c,fp)

putc(c,fp)

int c;

FILE *fp;

fp가 가르키는 화일로 한 문자 출력

fgetc(fp)

fgetc(c)

int c;

fp가 가르키는 화일로부터 한 문자 입력

fputc(c,fp)

fputc(c,fp)

int c;

FILE *fp;

fp가 가르키는 화일로 한 문자 출력

fgtes(s,n,fp)

fgtes(s,n,fp)

char *s;

int n;

FILE *fp;

화일 fp에 최대 n바이트의 문자열 s를 입력

fputs(s,fp)

fputs(s,fp)

char *s;

FILE *fp;

화일 fp에 문자열 s를 출력

fprintf(fp,format,인수)

fprints(fp,format,인수)

FILE *fp;

char *format;

화일 fp에 지정된 형태 format에 따라 출력

fscanf(fp,format,인수)

fscanf(fp,format,인수)

FILE *fp;

char *format;

화일 fp에서 지정된 형태 format에 따라 입력

fread(s,n,item,fp)

fread(s,n,item,fp)

char *s;

int n;

int item;

FILE *fp;

화일 fp에서 지정된 갯수 만큼의 데이타를 읽어 문자 열 s에 입력

 

 

연 습 문 제

 

[문제 11-1] 화일을 읽어서 대문자는 소문자로 소문자는 대문자로 변환하는 프로그램  

#include <stdio.h>

void main()

     {

          int ch;

          FILE *fp, *fp;

          char filename1[13], filename2[13];

          printf("\n Input filename : ");

          gets(filename1);

          fp=fopen(filename1,"r");

          printf(" Output filename : ");

          gets(filename2);

          fp1=fopen(filename2,"w");

          while((ch=fgetc(fp)) != EOF) {

               if(ch >= 'A' &&ch <='Z')

                    fputc(ch + 32, fp1);

               else if(ch >= 'a' &&ch <='z')

                    fputc(ch-32,fp1);

               else

                    fputc(ch,fp1);

          }

     }

Input filename : in_letter.dat

Output filename : out_letter.dat

c:>type in_letter.dat

Jim Botton, sportscaster and author of Ball Four, on

the reception to his book : "Pete rose would screan at

me from dugout because I recealed too much. Now he's

posing in magazine ads in his underwear.

c:>type out_letter.dat

jIM bOTTON, SPORTSCASTER AND AUTHOR OF bALL fOUR, ON

THE RECEPTION TO HIS BOOK : "pETE ROSE WOULD SCREAN AT

ME FROM DUGOUT BECAUSE i RECEALED TOO MUCH. nOW HE'S

POSING IN MAGAZINE ADS IN HIS UNDERWEAR.

 

 

 

[학습 11-1]  

#include <stdio.h>

void main()

     {

          int ch;

          int i=0;

          char string[100];

          printf("\n Input string : ");

          gets(string);

          do {

               ch=string[i];

               if(ch >= 'A' &&ch <='Z')

                    putchar(ch + 32);

               else if(ch >= 'a' &&ch <='z')

                    putchar(ch-32);

               else

                    putchar(ch);

               i++;

           } while(string[i] != '\0');

     }

Input String : This is Sample Text

tHIS IS sAMPLE tEXT  

 

 

[문제 11-2] 레코드 단위 입·출력 함수를 이용하여 봉급을 계산하는 프로그램  

#include <stdio.h>

#include <string.h>

struct{

     char depart[4];

     char name[20];

     int day;

     float pay;

}work_pay;

void main()

     {

          char numstr[20], flag;

          float money=30000;

          FILE *fp;

          if((fp=fopen("work_pay.rec","ab"))==NULL)

          fp=fopen("work_pay.rec","wb");

          do{

               printf("\nEnter department : ");

               gets(work_pay.depart);

               printf("Enter name : ");

               gets(work_pay.name);

               printf("Enter How much day : ");

               gets(numstr);

               work_pay.day=atoi(numstr);

               work_pay.pay=money * (float)work_pay.day;

               printf("\nPayment per day : %f",money);

               printf("\nTotal Payment:%f",work_pay.pay);

               fwrite(&work_pay,sizeof(work_pay),1,fp);

               printf("\n\nContinue (Y/N)...");

               flag=getche();

          }while(flag != 'N' &&flag != 'n');

          fclose(fp);

     }

Enter department : 3

Enter name : Kang jae sik

Enter how much day : 34

 

Payment per day : 30000.00000

Total Payment : 1020000.00000

Continue (Y/N)...

결과는 "work_pay.rec"라는 화일에 2진 정보로 저장된다.

 

 

[문제 11-3] 화일에 있는 문자의 수를 출력  

 #include <stdio.h>

#include <string.h>

void main(int argc, char *argv[])

     {

          FILE *fp;

          char string[81];

          char filename[13];

          int count=0;

          strcpy(filename,argv[1]);

          if(argc != 2) {

               printf("\n Input filename : ");

               gets(filename);

          }

          if((fp=fopen(filename,"r")) == NULL) {

               printf("\n File not found");

               exit(-1);

          }

          while(getc(fp) != EOF)

          count++;

          printf("\n File <%s >contains %d characters",filename,count);

          fclose(fp);

     }

 Input file name : exam.dat

File <exam.dat >contains 444 characters

 

 

[문제 11-4] 임의의 화일을 읽어서 행을 두 배로 확장하는 프로그램  

#include <stdio.h>

#include <string.h>

void main(int argc, char *argv[])

     {

          FILE *in_fp, *out_fp;

          char in_filename[13], out_filename[13];

          strcpy(in_filename,argv[1]);

          strcpy(out_filename,argv[2]);

          if(argc <3) {

               printf("\n Input filename : ");

               gets(in_filename);

               printf(" Output filename : ");

               gets(out_filename);

          }

          in_fp=fopen(in_filename,"r");

          out_fp=fopen(out_filename,"w");

          double_space(in_fp, out_fp);

          fclose(in_fp);

          fclose(out_fp);

     }

double_space(in_p, out_p)

FILE *in_p, *out_p;

     {

          int c;

          while((c=getc(in_p)) != EOF) {

               putc(c,out_p);

               if(c == '\n')

                    putc('\n',out_p);

          }

     }

 Input filename : in.dat

Output filename : out.dat

c:>type in.dat

This is

a

sample

file

c:>type out.dat

This is

a

sample

file 

 

 

[학습 11-2] 화일의 개행문자를 무시하여 화일의 내용을 한 줄로 길게 늘리는 프로그램  

#include <stdio.h>

#include <string.h>

void main(int argc, char *argv[])

     {

          FILE *in_fp, *out_fp;

          char in_filename[13], out_filename[13];

          strcpy(in_filename,argv[1]);

          strcpy(out_filename,argv[2]);

          if(argc <3) {

               printf("\n Input filename : ");

               gets(in_filename);

               printf(" Output filename : ");

               gets(out_filename);

          }

          in_fp=fopen(in_filename,"r");

          out_fp=fopen(out_filename,"w");

          double_space(in_fp, out_fp);

          fclose(in_fp);

          fclose(out_fp);

     }

double_space(in_p, out_p)

FILE *in_p, *out_p;

     {

          int c;

          while((c=getc(in_p)) != EOF) {

               if(c == '\n')

                    putc(' ',out_p);

               else

                    putc(c,out_p);

          }

     }

Input filename : in.dat

Output filename : out.dat

c:>type in.dat

This is

a

sample

file

c:>type out.dat

This is a sample file  

 

 

[문제 11-5] 화일에 'A'부터 'Z'까지의 문자를 저장하고 해당 위치에 있는 문자를 출력  

 #include <stdio.h>

void main()

     {

          FILE *fp;

          int ch;

          int num;

          char filename[13];

          printf("\n Input file name : ");

          gets(filename);

          fp=fopen(filename,"w");

          for(ch='A'; ch<='Z'; ch++)

          putc(ch, fp);

          fclose(fp);

          if((fp=fopen(filename,"r")) == NULL) {

               printf("\n File not found");

               exit(-1);

          }

          printf(" Record Number = ");

          scanf("%d",&num);

          if(fseek(fp,num-1,0) != 0) {

               perror("Can't move pointer there.");

               exit(-1);

          }

          ch=getc(fp);

          printf("\n Record Number : %d character = %c",num,ch);

          fclose(fp);

     }

 Input file name : in.dat

Record number : 5

Record number : 5 character = e

c:>type in.dat

ABCDEFGHIJKLMNOPQRSTUVWXYZ

 

 

[문제 11-6] 화일을 복사하는 프로그램  

#include <stdio.h>

#include <string.h>

void main(int argc, char *argv[])

     {

          FILE *in_fp, *out_fp;

          char in_filename[13], out_filename[13];

          char ch;

          strcpy(in_filename, argv[1]);

          strcpy(out_filename, argv[2]);

          if(argc <3) {

               printf("\n Input file name : ");

               gets(in_filename);

               printf(" Output file name : ");

               gets(out_filename);

          }

          if((in_fp=fopen(in_filename,"r")) == NULL) {

               printf("\n %s file not found");

               exit(-1);

          }

          out_fp=fopen(out_filename,"w");

          while((ch=getc(in_fp)) != EOF)

          putc(ch, out_fp);

          fclose(in_fp);

          fclose(out_fp);

     }

Input file name : letter.dat

Output file name : letter.bak

c:>type letter.dat

Jim Botton, sportscaster and author of Ball Four, on

the reception to his book : "Pete rose would screan at

me from dugout because I recealed too much. Now he's

posing in magazine ads in his underwear.

c:>type letter.bak

Jim Botton, sportscaster and author of Ball Four, on

the reception to his book : "Pete rose would screan at

me from dugout because I recealed too much. Now he's

posing in magazine ads in his underwear.

  

 

 

[학습 11-3] 두 개의 화일을 합병하는 프로그램  

 #include <stdio.h>

#include <string.h>

#define MAX_LINE 80

void main(int argc, char *argv[])

     {

          FILE *in_fp[2], *out_fp;

          int i;

          char ch;

          char temp[MAX_LINE];

          char *in_filename[2],

          out_filename[13];

          strcpy(in_filename[0],argv[1]);

          strcpy(in_filename[1],argv[2]);

          if(argc <3) {

               for(i=0; i<2; i++) {

                    printf("Input %d-st file name : ",i+1);

                    scanf("%s",in_filename[i]);

               }

          }

          for(i=0; i<=1; i++)

          if((in_fp[i]=fopen(in_filename[i],"r")) == NULL) {

               printf("\n %s file not found",in_filename[i]);

               exit(-1);

          }

          printf("\nMerge file name : ");

          scanf("%s",out_filename);

          out_fp=fopen(out_filename,"w");

          for(i=0; i<2; i++) {

               while((ch=getc(in_fp[i])) != EOF)

               putc(ch,out_fp);

          }

          fclose(in_fp[0]);

          fclose(in_fp[1]);

          fclose(out_fp);

     }

Input 1-st file name : in1.dat

Input 2-st file nsme : in2.dat

Merge file name : out.dat

c:>type in1.dat

Jim Botton, sportscaster and author of Ball Four, on

the reception to his book : "Pete rose would screan at

c:>type in2.dat

me from dugout because I recealed too much. Now he's

posing in magazine ads in his underwear.

c:>type out.dat

Jim Botton, sportscaster and author of Ball Four, on

the reception to his book : "Pete rose would screan at

me from dugout because I recealed too much. Now he's

posing in magazine ads in his underwear. 

 

 

[문제 11-7] 화일을 dump하는 프로그램  

#include <stdio.h>

#include <string.h>

void main(int argc, char *argv[])

     {

          FILE *fp;

          int i;

          char ch;

          char buffer[17];

          char filename[13];

          strcpy(filename,argv[1]);

          if(argc <2) {

               printf("\nInput file name : ");

               scanf("%s",filename);

          }

          if((fp=fopen(filename,"r")) == NULL) {

               printf("\n %d file not found",filename);

               exit(-1);

          }

          do {

               for(i=0; i<16; i++) {

                    ch=getc(fp);

                    if(ch == EOF)

                         exit(1);

                    if(ch <31)

                         ch='.';

                    buffer[i]=ch;

                    printf("%3X",ch);

               }

               buffer[i]='\0';

               printf(" %s",buffer);

               putchar('\n');

          } while(1);

          fclose(fp);

     }

Input file name : letter.dat

20 20 20 4A 69 6D 20 42 6F 74 74 6F 6E 2C 20 73 Jim Botton, s

70 6F 72 74 73 63 61 73 74 65 72 20 61 6E 64 20 portscaster and

61 75 74 68 6F 72 20 6F 66 20 42 61 6C 6C 20 46 author of Ball F

6F 75 72 2C 20 6F 6E 2E 20 74 68 65 20 72 65 63 our, on. the rec

65 70 74 69 6F 6E 20 74 6F 20 68 69 73 20 62 6F eption to his bo

6F 6B 20 3A 20 22 50 65 74 65 20 72 6F 73 65 20 ok : "Pete rose

77 6F 75 6C 64 20 73 63 72 65 61 6E 20 61 74 2E would screan at.

20 6D 65 20 66 72 6F 6D 20 64 75 67 6F 75 74 20 me from dugout

62 65 63 61 75 73 65 20 20 49 20 72 65 63 65 61 because I recea

6C 65 64 20 74 6F 6F 20 6D 75 63 68 2E 20 4E 6F led too much. No

77 20 68 65 27 73 2E 20 70 6F 73 69 6E 67 20 69 w he's. posing i

6E 20 6D 61 67 61 7A 69 6E 65 20 61 64 73 20 69 n magazine ads i

6E 20 68 69 73 20 75 6E 64 65 72 77 65 61 72 2E n his underwear.

2E 2E  

 

 

[문제 11-8] sin함수를 화일에 출력하는 프로그램  

#include <stdio.h>

#include <math.h>

void main()

     {

          FILE *fp;

          char filename[13];

          int num,i;

          float x;

          printf("\nOutput file name : ");

          scanf("%s",filename);

          fp=fopen(filename,"w");

          num=16;

          fprintf(fp,"%d",num);

          for(i=0; i<num; i++) {

               x=sin(2*M_PI/num*i);

               fprintf(fp,"\n%f",x);

          }

     }

Output file name : sin.dat

16

0.000000

0.382683

0.707107

0.923880

1.000000

0.923880

0.707107

0.382683

0.000000

-0.382683

-0.707107

-0.923880

-1.000000

-0.923880

-0.707107

-0.382683  

 

 

[학습 11-4] 위에서 구한 화일을 그래프 형식으로 출력하기  

 #include <stdio.h>

#include <math.h>

#define MAX 100

void main()

     {

          int n;

          float x[MAX];

          file_io(&n,x);

          disp(n,x);

          sgraph(n,x);

     }

disp(int num,float array[])

     {

          int i;

          for(i=0; i<num; i++)

          printf("%d\t%f\n",i,array[i]);

     }

sgraph(int num, float array[])

     {

          int i,j,k=39;

          float fmax=0;

          for(i=0; i<num; i++) {

               if(fabs(array[i]>fmax))

               fmax=fabs(array[i]);

          }

          for(i=0; i<num; i++)

               array[i]/=fmax;

          printf("-1");string(k-2,' ');

          printf("0");string(k-2,' ');

          printf("+1\n");printf("+"); string(k-1,'-');

          printf("+"); string(k-1,'-');

          printf("+\n");

          for(i=0; i<num; i++) {

               j=array[i]*k + 0.5 + k;

               if(j>k) {string(k,' ');printf("+");

               string(j-k-1,'-');printf("*\n");

          }

          if(k==j) {

               string(k,' ');

               printf("*\n");

 

          }

          if(j<k) {

               string(j,' ');printf("*");

               string(k-j-1,'-');printf("+\n");

          }

     }

string(int x,char y)

     {

          if(x >0)

          while(x--)

          putchar(y);

     }

file_io(int *num,float array[])

     {

          FILE *fp;

          int i;

          char filename[13];

          printf("\nInput file name : ");

          scanf("%s",filename);

          if((fp=fopen(filename,"r")) == NULL) {

               printf("\n %s file not found",filename);

               exit(-1);

          }

          fscanf(fp,"%d",num);

          for(i=0; i<*num; i++)

          fscanf(fp,"%f",&array[i]);

     }

 Input file name : sin.dat

00.000000

10.382683

20.707107

30.923880

41.000000

50.923880

60.707107

70.382683

80.000000

9-0.382683

10-0.707107

11-0.923880

12-1.000000

13-0.923880

14-0.707107

15-0.382683

-1 0

+1

+--------------------------------------+--------------------------------------+

*

+--------------*

+---------------------------*

+-----------------------------------*

+--------------------------------------*

+-----------------------------------*

+---------------------------*

+--------------*

*

*--------------+

*---------------------------+

*-----------------------------------+

*--------------------------------------+

*-----------------------------------+

*---------------------------+

*--------------+

표 11.5 표준 입․출력 함수표 11.4 랜덤 처리계 함수표 11.3 기준점의 위치표 11.2 화일 접근 모드표 11.1 <stdio.h>화일의 개방 스트림

Posted by 두장

Thread 사용시 UpdateData(false); 는 실행 중에 Debug Assertion Failed라는

에러는 발생시킨다.

예를 들면,

MyThread( CMyDlg *p )
{
p->UpdateData(BOOL);
}
위와 같은 방법은 않된다.

 

가능한 방법1.

GetDlgItem()->SetWindowText(CString); 방법을 사용해서 특정 아이템만

업데이트를 하는 것이다. 이때, SetWindowText()의 인자 값으로는 CString만 온다는 것에 주의.

예제.

  int cnt = 100;

  CString num;
  num.Format("%d",cnt);

  Pointers->GetDlgItem(IDC_STATIC_RESULT)->SetWindowText(num);

 


그러나 콘트롤 하나 하나 직접 처리하는게 번거로워
UpdateData()로 일괄처리하고 싶으시다면..

유저메시지를 하나 만들어서 윈도로 쏘세요.. PostMessage( UM_UPDATE) 따위로
..
그래서 그 윈도가 스스로 UpdateData를 실행하도록 하셔야 합니다
.

예를 들면
..
#define UM_UPDATE WM_USER

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    ON_MESSAGE( UM_UPDATE, OnUpdateData)
END_MESSAGE_MAP()

LRESULT CMyDlg::OnUpdateData( WPARAM wParam, LPARAM lParam)
{
    UpdateData( FALSE);

    return 0;
}

위와 같이 해 놓고.. 필요할때..스레드 상에서 다음과 같이 호출하면 되겠지요
.

pDlg->PostMessage( UM_UPDATE);

pDlg
CMyDlg의 포인터입니다. CMyDlg가 매인 윈도우라면

AfxGetApp()->m_pMainWnd
으로 얻을 수 있으며,
아니라면 스레드의 파라메터로 넘겨받아 쓰면 되죠
.

참고로 MFC가 모든 스레드 환경에서 완전하게 작동하진 않습니다
.
스레드로 뭔가를 할 때는 꼭 염두에 두셔야 해요.. -_-;

 

Posted by 두장

//////////////////////////////////////////////////////////////////////////
// BSTR을 Char *으로 변환 하는 함수
// 필수 Include file : atlconv.h
//////////////////////////////////////////////////////////////////////////
// Made by kong
// 2006. 11. 16.
//////////////////////////////////////////////////////////////////////////
void BSTRtoCHAR(char *Msg, const BSTR conv)
{
      USES_CONVERSION;
      strcpy( Msg, OLE2T(conv) );
}

//////////////////////////////////////////////////////////////////////////
// Char *를 BSTR로 변경
void CHARtoBSTR( BSTR *Msg, const char *conv )
{
      USES_CONVERSION;
      *Msg = T2OLE(conv);

      // *Msg = SysAllocString( A2W(conv) );
}


추신 : 근데 간혹 컴파일 에러나면 *Msg = T2OLE(conv);를 주석하시고

         밑에 *Msg = SysAllocString( A2W(conv) );를 주석 풀어주시면 됩니다.

           대신 SysAllocString을 해주시면 나중에 SysFreeString을 해주어야 합니다.^^

Posted by 두장
이전버튼 1 2 이전버튼