Text Class を作成-1

Console Mode で TEXT FILE を扱う Text Object Class を作成します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. 新規プロジェクトから、空の Console Application を作成して下さい。
    プロジェクトのフォルダーに次のファイルを格納して、[プロジェクト][既存項目の追加] からプロジェクトに追加します。
    一般的な Object Class はヘッダファイルとプログラムファイルに分ける方が一般的です。
    ファイル名 説明
    Main.cpp メインプログラムのファイル
    TextClass.h Text Class のヘッダファイル
    TextClass.cpp Text Class のプログラムファイル
  2. Main.cpp のプログラムです。
    "TextClass.h" を取り込んで下さい。
    入力領域などは TextClass.h で宣言しています。
    /*★ Text Object Class Main Program    前田 稔 ★*/
    #include <stdio.h>
    #include <conio.h>
    #include "TextClass.h"
    
    //Main Program
    int  main()
    {   TextClass   *tclass= NULL;
        char        file[256];
    
        tclass= new TextClass();
        printf("入力ファイル名をタイプして下さい\n");
        scanf_s("%s",file,256);
        tclass->GetText(file);
        tclass->PutMsg(100);
        printf("----------------------------------------\n");
        tclass->PutMsg((tclass->m_P+50),100);
        printf("----------------------------------------\n");
        tclass->PutMsg();
        _getch();
        if (tclass)     delete tclass;
    }
    
  3. Text Class のヘッダファイル TextClass.h です。
    入出力領域とそのサイズ、及びメンバ関数を定義します。
    PutMsg() を3通りの方法でオーバーロードしてみました。
    実際に印字しているのは PutMsg(char *p, unsigned int siz); だけで、他はパラメータを設定して呼び出すだけです。
    このようにクラスを定義するブロックの中で実装すると inline 関数となります。
    inline 関数の説明は inline 関数 を参照して下さい。
    /*★ Text Class Header File   前田 稔 ★*/
    #define SIZE    32766
    
    class  TextClass
    {
      protected:
      public:
        char            m_Buf[SIZE+2];  //入出力バッファ
        char            *m_P;           //入出力バッファポインタ
        unsigned int    m_Len;          //入力長
    
        TextClass();                    //Constructor
        unsigned int    GetText(char *file);
        void            PutMsg(char *p, unsigned int siz);
        void            PutMsg()  { PutMsg(m_P,m_Len); };
        void            PutMsg(unsigned int siz)  { PutMsg(m_P,siz); };
    };
    
  4. Text Class のプログラムファイル TextClass.cpp です。
    Constructor では *m_P の方が使い勝手が良さそうなので、m_Buf[] の先頭アドレスを設定しています。
    PutMsg() では、文字列を printf() で印字するために、TEXT 文字列の最後の印として '\0' を格納しています。
    印字が終わると '\0' を元の値に戻します。
    /*★ Text Class Program File   前田 稔 ★*/
    #include <conio.h>
    #include "TextClass.h"
    
    //Constructor
    TextClass::TextClass()
    {   m_P= (char*)m_Buf;
    }
    
    //Get Text File
    unsigned int  TextClass::GetText(char *file)
    {   FILE    *FI;            //FILE の定義
    
        if (fopen_s(&FI,file,"rb")!=0)
        {   printf("INPUT FILE open error[%s]\n",file);
            return 0;
        }
        m_Len= fread(m_P,1,SIZE,FI);
        *(m_P+m_Len)= '\0';
        fclose(FI);
        return m_Len;
    }
    
    //Text Buffer の表示
    void  TextClass::PutMsg(char *p, unsigned int siz)
    {   char wk;
    
        wk= *(p+siz);
        *(p+siz)= '\0';
        printf("%s\n",p);
        *(p+siz)= wk;
    }
    
  5. Main.cpp ではファイル名をタイプ入力して、tclass->GetText(file) でデータを入力します。
    入力したテキストを、3通りの PutMsg(); で表示しています。
    最初は、先頭から 100 文字表示します。
    次に、先頭+50 の位置から 100 文字表示します。
    最後に、先頭から全ての文字を表示します。
        tclass->PutMsg(100);
        tclass->PutMsg((tclass->m_P+50),100);
        tclass->PutMsg();
        

超初心者のプログラム入門(C/C++)