'터보 C 정복' 에 있는 파일 입출력 예제에서요..

yangam의 이미지

// file copy program (only text file, 992 page)

#include <stdio.h>

#define BUFMAX	255		// MAX: 255 characters per line

int main(int argc, char *argv[])
{
	char str[BUFMAX];
	FILE *src, *des;

	if (argc <= 2)
		printf("Usage: %s source destin\n", argv[0]), exit(1);

	if ((src = fopen(argv[1], "rt")) == NULL)
		perror("Source"), exit(1);

	if ((des = fopen(argv[2], "wt")) == NULL)
		perror("Destin"), exit(1);

	while (fgets(str, BUFMAX, src) != NULL)
	{
		if (fputs(str, des) == EOF)
			break;
	}

	if (ferror(src) || ferror(des))
		perror("Copying"), exit(1);

	else 
		fputc('\x1a', des);			// 여기서 손수 Ctrl-Z 문자를 덧붙여야 한다.

	if (fclose(des) == EOF)
		perror("Desting"), exit(1);

	else
		printf("Text file \"%s\" copied to \"%s\"\n", argv[1], argv[2]), exit(1);
		
	return 0;
}

else
fputc('\x1a', des);

이 문장에 대해서 질문이 있습니다.
왜 굳이 저렇게 ^Z 문자를 추가해야 하는거죠?

책에서는 이렇게 적혀있습니다.

------------------------------------------------------------------------------
텍스트 스트림에서 논리적인 화일끝을 나타내는 ^Z 문자('\x1a') 는
EOF 로 변환되어 읽혀진다. EOF 는 stdio.h 에 정의되어있는 매크로
상수이며, 실제값은 -1 이다. 반면에 텍스트 스트림에다 ^Z 문자를
쓸 때에는 이런 변환이 발생하지 않으며, 이진 스트림에서는 읽기, 쓰기를
막론하고 이러한 변환이 전혀 발생하지 않는다.
------------------------------------------------------------------------------

그런데 텍스트 화일의 끝에 꼭 ^Z 가 있어야 하나요?
vi 에디터로 저장한 파일의 끝에도 ^Z 가 존재하지 않던데..;;
제가 아직 파일에 대해서 자세히 몰라서요;
좀 해메게 되네요.

답변 부탁드립니다.

moonzoo의 이미지

필자의 의도는 알수 없으나..

copy하는데는 없어도 됩니다.

그 부분 빼고 직접 돌려보세요.

sjpark의 이미지

도스시절에는 텍스트파일을 콘솔에서 작성하고 저장하기 위해(EOF를 알리기 위해) CTRL+Z를 사용했습니다.

물론, CTRL+Z로 EOF를 알렸기 때문에, feof등의 함수를 사용하지 않고, 도스에서의 파일의 끝(EOF)는 CTRL+Z가 있는지 없는지로 판단할 수 있다고 본것 같습니다.

터보 C 정복은 오래된 서적 같은데..
(저도 갖고 있긴 하지만 요세 보는일은 드물죠.. 아마 거기선 에디터로 Turbo-C를...사용한다는...)

cdpark의 이미지

"터보 C 정복". 좋은 책입니다만, 적당히 걸러읽어야 할 부분도 많은 책입니다.

moonzoo의 이미지

"터보 C정복" 입문용으로 괜찮은 책이라고 생각합니다.

옛날 생각나네요.. 핵교 다닐때 탱자 탱자 놀다가..

군대가서... 말년에 공부했던 책이라서 애착이 남아있는 책.

좀 두꺼웠던 걸로 기억이 납니다.

책이 두꺼운 만큼 설명이 좀 돼있어서..처음 C를 할때는

좋았던 것 같습니다.

그 후로 본책으로는 UNIX Systems Programming for SVR4였는데

아주 좋았습니다.

댓글 달기

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