fread(), fwrite() 에서 size, nmemb 의 의미는?

mithrandir의 이미지

       size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

       size_t   fwrite(const  void  *ptr,  size_t  size,  size_t  nmemb,  FILE
       *stream);

함수 정의는 위와 같습니다.

Quote:

The function fread reads nmemb elements of data, each size bytes long,
from the stream pointed to by stream, storing them at the location
given by ptr.

The function fwrite writes nmemb elements of data, each size bytes
long, to the stream pointed to by stream, obtaining them from the loca-
tion given by ptr.

그리고 설명은 이와 같습니다. size 크기만큼의 덩어리를 nmemb 갯수만큼 읽는 다는 의미로 이해했습니다. 그런데 실제 내부 구현을 보면,

size_t
fread (p, size, nmemb, stream)
     void *p;
     size_t size;
     size_t nmemb;
     register FILE *stream;
{
  register char *ptr = (char *) p;
  register size_t to_read = size * nmemb;
  size_t bytes = to_read;

.
.
중략
.
.


  return (bytes - to_read) / size;
}     

이고, 실제로 nmemb나 size를 직접 사용하는 부분이 없습니다. return값만 size가 몇개 읽어졌나를 반환하는 셈이지요. 여기서 궁금해졌습니다. size와 nmemb 를 다르게 해서 반환값이 바뀌는 것은 함수를 부르고 나서도 간단히 얻을 수 있는 값인데요. 어째서 size, nmemb 를 구분하는 것일까요?

역사적인 이유라도 있는 것일까요?

vuccell의 이미지

잘 모르겠습니다.... 만... (아~ 이런 전제를 할수밖에 없는 초보 -,-;;; )

malloc에서 sizeof와 count를 곱해서 메모리할당하고,
calloc에서는 각각의 인자를 받아서 처리하는 차이가 아닐까요?

만약에 두개를 합쳐서 쓰고 싶으면 그냥 write함수를 이용하시면 되지 않을까요?

익명 사용자의 이미지

fread, fwrite는 정형화된 structure단위로 읽고 쓰기 편하게 되어있습니다.

예를 들어 파일의 각 line이

이름 학번 주소

위와 같이 되어 있다면

typedef structure
{
   char name[16];
   char id[16];
   char address[16];
   char null[1];
}STUDENT_INFO;

STUDENT_INFO si[NUM];

.. si의 내용을 채우고..

fwrite( &si, sizeof(si), NUM, fp); 로 쓰고..
fread( &si, sizeof(si), NUM, fp); 로 읽으면 됩니다..

위와 같이 structure등을 다룰 때는 read나 write를 쓰는 것보다

훨씬 간단함을 알 수 있습니다.

댓글 달기

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