ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 상속과포함_20080820_20:60
    Programming/C++ 언어 2008. 8. 20. 20:07
     
    ========================== *.txt 문서저장 클래스 ==========================================================   docwriter.h  파일 ==
     
    #ifndef DOCWRITER_H
    #define DOCWRITER_H

    #include <string>
    using namespace std;

    class DocWriter
    {
    public:
      DocWriter();
      DocWriter(const string& fileName, const string& content);
      ~DocWriter();

      void SetFileName(const string& fileName);             // 파일 이름을 지정

      void SetContent(const string& content);               // 저장할 텍스트를 지정

      void Write();                                         // 파일에 텍스트를 저장시킨다.

    protected:
      string _fileName;
      string _content;
    };

    #endif
     
     
     
    ========================== *.txt 문서저장 클래스 ==========================================================   docwriter.cpp  파일 ==
     
    #include "docwriter.h"
    #include <fstream>
    using namespace std;


    DocWriter::DocWriter()
    {
      // 파일이름, 텍스트를 디폴트로 지정
      _fileName = "NoName.txt";
      _content = "There is no content.";
    }

    DocWriter::DocWriter(const string& fileName, const string& content)
    {
      _fileName = fileName;
      _content = content;
    }

    DocWriter::~DocWriter()
    {
    }

    // 파일 이름을 지정
    void DocWriter::SetFileName(const string& fileName)
    {
      _fileName = fileName;
    }

    // 저장할 텍스트를 지정
    void DocWriter::SetContent(const string& content)
    {
      _content = content;
    }

    // 파일에 텍스트를 저장시킨다.
    void DocWriter::Write()
    {
      // 파일을 연다.
      ofstream of( _fileName.c_str() );

      // 헤더문구 저장.
      of << "# Content #\n\n";

      // 문자열 저장저장한다.
      of << _content;
    }

     
    ========================== *.txt 문서저장 클래스 ==========================================================   example.cpp  파일 ==
     
    #include "docwriter.h"

    int main()
    {
      DocWriter dw;
      dw.SetFileName( "test.txt" );
      dw.SetContent("You must be a good programmer~!!");
      dw.Write();

      return 0;
    }
     
     
    ========================== *.txt 문서저장 클래스 ==========================================================   실행후  test.txt  파일내용==
     
    # Content #
     
    You must be a good programmer~!!
     
     
     
     
     
    ################################################################################################################
     
    ========================== *.html 문서저장 클래스 ==========================================================   htmlwriter.h  파일 ==
     
    #ifndef HTMLWRITER_H
    #define HTMLWRITER_H

    #include "docwriter.h"

    class HTMLWriter : public DocWriter
    {
    public:
      HTMLWriter();
      ~HTMLWriter();

      // 텍스트를 파일로 저장시킨다.
      void Write();

      // 폰트를 지정한다.
      void SetFont(const string& fontName, int fontSize, const string& fontColor);


    protected:
      string  _fontName;
      int    _fontSize;
      string  _fontColor;
    };

    #endif

     
    ========================== *.html 문서저장 클래스 ==========================================================   htmlwriter.cpp  파일 ==
     
    #include "htmlwriter.h"
    #include <fstream>
    using namespace std;

    HTMLWriter::HTMLWriter()
    {
      // 디폴트 파일 이름만 바꾼다.
      _fileName = "NoName.html";

      // 디폴트 폰트를 지정한다.
      _fontName = "굴림";
      _fontSize = 3;
      _fontColor = "black";
    }

    HTMLWriter::~HTMLWriter()
    {
    }

    // 파일에 텍스트를 저장시킨다.
    void HTMLWriter::Write()
    {
      // 파일을 연다.
      ofstream of( _fileName.c_str() );

      // HTML 헤더 부분을 저장한다.
      of << "<HTML><HEAD><TITLE>This document was generated by HTMLWriter</TITLE></HEAD><BODY>";
      of << "<H1>Content</H1>";

      // 폰트 태그를 시작한다.
      of << "<Font name='" << _fontName << "' size='" << _fontSize << "' color='" << _fontColor << "'>";

      // 텍스트를  저장한다.
      of << _content;

      // 폰트 태그를 닫는다.
      of << "</FONT>";

      // HTML을 마무리 한다.
      of << "</BODY></HTML>";
    }

    // 폰트를 지정한다.
    void HTMLWriter::SetFont(const string& fontName, int fontSize, const string& fontColor)
    {
      _fontName = fontName;
      _fontSize = fontSize;
      _fontColor = fontColor;
    }

    ========================== *.html 문서저장 클래스 ==========================================================   example.cpp  파일 ==
     
    #include "htmlwriter.h"

    int main()
    {
      HTMLWriter hw;
      hw.SetFileName( "test.html" );
      hw.SetContent("You must be a good programmer~!!");
      hw.SetFont("Arial", 16, "blue");
      hw.Write();

      return 0;
    }

    ========================== *.txt 문서저장 클래스 ==========================================================   실행후 test.html  파일내용==
     

    Content

    You must be a good programmer~!!

    댓글

Designed by Tistory.