cat 명령을 C로 구현하는 도중에 질문있습니다.

ratmhun의 이미지

유닉스 시스템 프로그래밍이라는 책을 보면서 cat 명령어를 만들어보고 있습니다.
-n 옵션에 대한 처리는 이해가 되는데요, -b, -s 옵션을 처리할 때에 빈 줄을 인식하는 부분이 이해하기가 힘듭니다. (-n 옵션은 cat 명령으로 파일 내용을 출력할 때에 줄번호를 붙이는 옵션이며 지금 구현하고자 하는 -b 옵션은 파일 내용을 출력할 때에 비어있는 라인에 대해서는 줄번호를 생략하는 옵션입니다.)
즉,

while((c = getc(fp)) != EOF)
{
...-n 옵션 처리...

if(b_flag == ON && c == '\n')
{
while((c = getc(fp)) == '\n');
putchar('\n');

ungetc(c, fp);
if((c = getc(fp)) == EOF)
break;
printf("\n%d\t", ++count);
}

....-s 옵션처리...

putc(c, stdout);
....

저자의 설명에는 'while((c = getc(fp)) == '\n');'와 관련된 설명이 단순히 '비어있는 라인'이라고만 되어 비어있는 라인은 라인번호를 출력하지 않고 putchar('\n')으로 처리한다고 되어 있습니다.

이미 바로 앞에 있는 줄에서 c의 값이 '\n' 값이라고 판단되어 있는 상태에서 다시 while문을 쓰는 이유는 무엇입니까?

어떻게 'while((c = getc(fp)) == '\n');'이 빈 라인으로 처리되는 것인지 궁금합니다.

송효진의 이미지

while((c = getc(fp)) == '\n'); ; 로 끝나서 이것 자체로 루프지요. \n 이 아닐때까지 계속 돌겠지요. \n\n\n\n\n\n\n\n\n\n 이라도 출력없이 루프돌다가 \n 이 아닌것이 나오면 루프를 나와서 putchar("\n");

litdream의 이미지

아래를 참고해 보신다면, 도움이 되실것입니다.
( 저도 예전에 어느분이 링크 걸어주신겁니다. )

http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/cat/

삽질의 대마왕...

ratmhun의 이미지

제가 잘못 올렸었네요...
while()에는 ';'가 없습니다;;;;;;;;
누가 해석좀 해주세요~

while((c = getc(fp)) != EOF)
{
...-n 옵션 처리...

if(b_flag == ON && c == '\n')
{
while((c = getc(fp)) == '\n')
putchar('\n');

ungetc(c, fp);
if((c = getc(fp)) == EOF)
break;
printf("\n%d\t", ++count);
}

....-s 옵션처리...

putc(c, stdout);
....

alfalf의 이미지

while((c = getc(fp)) == '\n') putchar('\n');

파일로 부터 한 글자를 읽어 들여 그게 '\n'이면 '\n'을 출력하고 다음 글자를 읽어 들이는 작업을 반복하다 '\n'이 아닌 글자가 오면 while 문의 조건이 false가 되니까 loop를 빠져나와 다음 아래의 명령을 수행하도록 하는겁니다. 그러니까 1개 이상의 빈줄이 연속으로 나올 경우를 대비해 while loop로 처리하는 것 같네요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.