Buffers in standard functions

This is a discussion on Buffers in standard functions within the C++ Programming forums, part of the General Programming Boards category; In a standard function like: Code: size_t fwrite(const void *buf, size_t size, size_t count, FILE *stream); How does the function ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    Buffers in standard functions

    In a standard function like:
    Code:
    size_t fwrite(const void *buf, size_t size, size_t count, FILE *stream);
    How does the function cycle through *buf? Does it use the '+' operator? '++'? Is it implemention-defined? I'm trying to write a linked list class that overloads the necessary operators to work in such a function. So I need to know how such a function works internally.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > How does the function cycle through *buf?
    Casts it back into unsigned char*
    The interface is void* so you can safely point at whatever without getting lots of warnings.

    Well you could if you were using it in C, but I notice this is posted on the C++ board.

    > I'm trying to write a linked list class that overloads the necessary operators to work in such a function.
    Overload the << and >> operators then?
    cout << myLinkedListNode;

    As prelude has just pointed out in another recent post, you can't simply fwrite() a block of memory if your type isn't a POD. You need a proper method of serialising your data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So I need to know how such a function works internally.
    It works the wrong way for what you're trying to do. Consider a naive implementation:
    Code:
    #include <cstdio>
    
    using namespace std;
    
    size_t jsw_fwrite ( const void *buf, size_t size, size_t count, FILE *stream )
    {
      if ( size == 0 )
        return 0;
    
      const unsigned char *p = static_cast<const unsigned char *> ( buf );
      size_t n = 0;
    
      for ( ; n < count; n++ ) {
        for ( size_t i = 0; i < size; i++ )
          fputc ( *p++, stream );
    
        if ( ferror ( stream ) )
          break;
      }
    
      return n;
    }
    No amount of operator overloading will make that work for a non-POD type.
    My best code is written with the delete key.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Balls.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. list of standard c functions
    By ElemenT.usha in forum C Programming
    Replies: 7
    Last Post: 01-01-2008, 10:19 AM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Allegro and standard functions
    By Paninaro in forum Game Programming
    Replies: 1
    Last Post: 07-05-2002, 05:41 PM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21