C언어 html문서 읽어서 들여쓰기 하려고 합니다. 조언 부탁 드립니다.

lhs8421478의 이미지

안녕하세요 C언어를 공부중인 청년입니다.

현재 리눅스 환경에서 gcc로 컴파일 하고 있으며

html 문서를 읽어서 tag를 구별하여 들여쓰기를 하는 프로그램을 만들고 있습니다.

진행상황은 html문서를 읽어서 tag를 구별하는단계까지 했습니다.

여기서 어떻게 나아가야할지 몰라서 이렇게 글을 올립니다.

현재 소스코드 상황은

/*
	file : tag_find.c
	return : 성공 0 , 실패 -1
	html문서를 읽어 들여 tag만 추출해서 txt파일에 저장하는 프로그램
*/
#include <stdio.h>
#include <string.h>
 
void tag_find(FILE *html, FILE *txt);
 
/*
	main 함수 
	인자값으로 오픈할 html 파일명과 저장할 txt 파일명을 받는다.
	받아온 이름의 파일을 오픈한다.
*/
int main(int argc, char **argv)
{
	FILE *html_file;
	FILE *txt_file;
 
	html_file = fopen(argv[1], "r");
	if (html_file == NULL) {
		printf("html_file open error\n");
		return -1;
	}
 
	txt_file = fopen(argv[2], "w");
	if (txt_file == NULL) {
		printf("txt_file open error\n");
		return -1;
	}
 
	tag_find(html_file, txt_file);
 
	fclose(html_file);
	fclose(txt_file);
	return 0;
}
 
/*
	tag_find 함수
	인자값으로 html 파일 포인터와 txt 파일 포인터를 받아온다.
	인자값으로 받아온 html파일에서 한글자씩 읽어들여 배열에 저장하고
	tag들을 찾아서 txt파일에 copy한다.
*/
void tag_find(FILE *html, FILE *txt)
{
	char tag[50];
	char *buf_pt;
	char ch;
	int cnt;
 
	cnt = 0;
	while (EOF != (ch = fgetc(html))) {
		buf_pt = tag;
		if (ch == '<') {
			memset(tag, 0x00, 50);
			*buf_pt = ch;
			buf_pt++;
			while ((ch = fgetc(html)) != '>') {
				*buf_pt = ch;
				buf_pt++;
			}
			*buf_pt = ch;
			cnt++;
			fprintf(txt, "%2d : %s\n", cnt, tag);
			printf("%2d : %s\n", cnt, tag);
		}
	}
}

이렇게 코딩하여 txt문서에 tag들만 따로 출력하게 만들어놨습니다.

이걸 바꺼서

<head>
<title>New Document</title>
</head>

이런 html 문서를

   <head>
      <title>
         New Document
      </title>
   </head>

이렇게 하려고 합니다...

조언 부탁 드립니다...

lhs8421478의 이미지

<html>
<head>
<title>New Document</title>
</head>
</html>

이런 html 문서를
<html>
   <head>
      <title>
         New Document
      </title>
   </head>
</html>

이렇게 하려고 합니다...
qiiiiiiiip의 이미지

1. 주 목적이 html을 reformatting 하시고 싶으신거라면 기존 툴을 쓰세요
예를 들어 http://tidy.sourceforge.net/
( 웬만한 에디터 (eg. vi) 도 이 정도 indentation은 간단히 해 줍니다.)

2. 주 목적이 c언어를 공부하고 싶으신 거라면 책을 사서 차근차근 보세요

3. 주 목적이 둘 다인 경우라면(당연히 이 경우 같네요), 더 중요한 목적 한가지만 고르세요.
지금 상황으로는 둘 다 놓칠 가능성이 훨씬 커 보이네요.

익명 사용자의 이미지

'<' 다음에 '/'인지 검사해서 여는 tag과 닫는 tag를 구분하세요.
여는 tag가 오면 level을 1 증가, 닫는 tag가 오면 level을 1 감소.
tag를 출력하기 전에 level 만큼 공백이나 탭 출력.

chadr의 이미지

스택은 이럴때 이용하라고 있는 것입니다. 스택을 이용해보세요.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.