help me understand setvbuf function

This is a discussion on help me understand setvbuf function within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFSIZE 1024 #define SLEN 81 void append(FILE *source, FILE *dest); int main(void) ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    help me understand setvbuf function

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    #define BUFSIZE 1024
    
    #define SLEN 81
    
    void append(FILE *source, FILE *dest);
    
    
    
    int main(void)
    
    {
    
        FILE *fa, *fs;// fa for append file, fs for source file
    
        int files = 0;  // number of files appended
    
        char file_app[SLEN];  // name of append file
    
        char file_src[SLEN];  // name of source file
    
        puts("Enter name of destination file:");
    
        gets(file_app);
    
        if ((fa = fopen(file_app, "a")) == NULL)
    
        {
    
            fprintf(stderr, "Can't open %s\n", file_app);
    
            exit(2);
    
        }
    
        if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
    
        {
    
            fputs("Can't create output buffer\n", stderr);
    
            exit(3);
    
        }
    
        puts("Enter name of first source file (empty line to quit):");
    
        while (gets(file_src) && file_src[0] != '\0')
    
        {
    
            if (strcmp(file_src, file_app) == 0)
    
                fputs("Can't append file to itself\n",stderr);
    
            else if ((fs = fopen(file_src, "r")) == NULL)
    
                fprintf(stderr, "Can't open %s\n", file_src);
    
            else
    
            {
    
                if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)
    
                {
    
                    fputs("Can't create input buffer\n",stderr);
    
                    continue;
    
                }
    
                append(fs, fa);
    
                if (ferror(fs) != 0)
    
                    fprintf(stderr,"Error in reading file %s.\n",
    
                            file_src);
    
                if (ferror(fa) != 0)
    
                    fprintf(stderr,"Error in writing file %s.\n",
    
                            file_app);
    
                fclose(fs);
    
                files++;
    
                printf("File %s appended.\n", file_src);
    
                puts("Next file (empty line to quit):");
    
            }
    
        }
    
        printf("Done. %d files appended.\n", files);
    
        fclose(fa);
    
    
    
        return 0;
    
    }
    
    
    
    void append(FILE *source, FILE *dest)
    
    {
    
        size_t bytes;
    
        static char temp[BUFSIZE]; // allocate once
    
        while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)
    
            fwrite(temp, sizeof (char), bytes, dest);
    
    }
    can someone explain to me how the setvbuf() function works? and also the append function in this sample program thanks..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Read:
    http://linux.die.net/man/3/setvbuf

    If you still have questions, please ask.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Also read the FAQ on why gets() is bad news.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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