fseek를 사용하여 eof뒤로 밀면?

익명 사용자의 이미지

#include

main()
{
FILE *fp;

fp = fopen("./ftest.txt", "r+");

printf("start..pos->[%ld]\n", ftell(fp));

fseek(fp, 20, SEEK_SET);
printf("now..pos->[%ld]\n", ftell(fp));

fprintf(fp,"this..");
printf("end..pos->[%ld]\n", ftell(fp));

}

위의 소스를 실행하면...

start..pos->[0]
now..pos->[20]
end..pos->[26]

위와 같은 결과가 나옵니다.

처음에 열기전의 파일 크기는 9였고
위의 프로그램을 실행한후 파일크기는 26이었습니다.

그리고 그 결과 파일을 vi로 열어보면..

123456789^@^@^@^@^@^@^@^@^@^@^@this..
~

위와 같은 값이 들어갑니다...
원래 있던 내용은 123...9이고 this.. 은 추가된 내용입니다.

중간에 이상한 값이 들어갔구요...

이런경우 상관 없나요??

익명 사용자의 이미지

123456789^@^@^@^@^@^@^@^@^@^@^@this..

VI에서 ^@가 한 글자(1byte)로 나타나는 것은 NULL을 의미합니다.

^A는 0x01이고.. 뭐 이런식으로 표현이 되죠.

원래 크기인 9bytes에서 강제로 20으로 만들었으니.. 11bytes의

NULL이 패딩된 것입니다. 어떤 형태로 코딩을 하시는지는 모르겠지만.

불필요한 11bytes를 패딩시켜가며 파일을 생성하는것 보단

한 바이트정도의 구분자를 주어 사용하는것이 좋을 듯 합니다.

익명 사용자의 이미지


fseek는 퍼일 크기 안쪽에서만 사용하세요.
EOF를 넘어가면 오떤 일이 벌어지는지는
아무도 책임을 지지 않는 것으로 알고 있습니다.

9바이트 파일에서 26바이트 파일로 만들고
싶다면 fwrite를 이용해서 갱신한 후 fseek를
사용하세요.

익명 사용자의 이미지

참고로, 파일의 맨 끝에서 fseek를 사용하여 사이즈를 증가시키면

님의 경우 처럼 널 문자가 추가가 됩니다. 이 널 문자가 한 블럭

을 이루면 hole이 됩니다.

이 hole은 실제 파일의 내용은 없지만, 블럭을 차지하게 됩니다.

hole를 가진 파일을 cp 명령을 통해 복사하면, hole이 유지가 됩

니다. 즉 불필요한 한 블럭이 복사가 되는 것이지요..

이런 경우엔, hole를 처리해 주면 한 블럭의 낭비를 줄일 수 있겠죠..

그럼 수고하세요

댓글 달기

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