본문 바로가기
C 언어

C언어 프로그램을 HTML 형식으로 만들기

by treeCoder 2021. 1. 3.

C언어 프로그램을 HTML 형식으로 만드는 프로그램이다.

 

다음 프로그램을 c2html.c 로 저정하고 compile 한다.

 

#include <stdio.h>

char first[ ] = "<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">";

char lb[]="&lt;" ;
char rb[]="&gt;" ; 
char amp[]="&amp;" ; 
char sq[]="&#39;" ;
char dq[]="&quot;" ;
char nl[]="</span>\n<span>" ;

char last[ ] = "</code></pre>\n</div>";

          
int main( ) {
    int c;

    printf("%s\n", first);
    
    printf("<span>");

    while( (c=getchar( )) != EOF ) {
             if( c == '<' ) printf("%s", lb);        /* < */
             else if( c == '>' ) printf("%s", rb);  /* > */
             else if( c == '&' ) printf("%s", amp); /* ampersand */
             else if( c == 39 ) printf("%s", sq); /* single quote */
             else if( c == 34 ) printf("%s", dq); /* double quote */
             else if( c == '\n' ) printf("%s", nl); /* new line */ 
             else
                putchar(c);
    }
    
    puts( last );
    
    return 0;

}

 

그리고 test.c 에는 다음 프로그램을 작성한다.

   #include <stdio.h>

   int main() {
  
       puts("Hello world\n");

       return 0;
   }

 

c2html < test.c 를 실행한다. 이 경우 코맨드 창에 아래와 같이 출력이 된다.

 

c2html < test.c > test.html 를 실행하면 코맨드 창에 아래와 같이 출력이 되지 않고,  test.html에기록이 된다.

 

test.html을 열어보면 아래와 같이 코드가 HTML로 보기 좋게 표현됨을 알 수 있다.

#include <stdio.h>

int main() {
  
    puts("Hello world\n");

    return 0;
}

댓글