Question about fwrite() function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seia0106

    Question about fwrite() function

    Hello

    In the course of writing a program. I have to read and write a media
    file(*.avi/*.mpg/*.wav) to and from memory buffer. I can put the
    data(from a *.avi file)in buffer successfully but when i try to write
    data from buffer into a new file, then i can not play that file. A
    file size of only 4kb is copied and i can not play the file.
    The size of original file, whose contents were copied into buffer was
    996KB.
    Can anyone please help me solve this problem. Why only 4KB are written
    into file. I guess may be the problem is in third paramater of
    fwrite() function 'NumberOfElemen ts', but i am not sure. Here is my
    simple function
    pBuffer was declared as global variable of type BYTE

    void WriteMediaFile( const char file_name[]) {
    /* Local variable declarations: */

    FILE *outdata;


    if ((outdata = fopen(file_name , "w")) == NULL) {
    fprintf(stderr, "***> Open error reading input file %s",
    file_name);
    exit(-1);
    } /* end if */

    fwrite(&pBuffer , sizeof(pBuffer) , 1 , outdata);



    fclose(outdata) ;
    } /* end function */
  • Jakob Bieling

    #2
    Re: Question about fwrite() function

    "seia0106" <miahmed67@yaho o.com> wrote in message
    news:4fe296bd.0 307170450.32253 38c@posting.goo gle.com...
    [color=blue]
    > pBuffer was declared as global variable of type BYTE[/color]

    What kind of type is BYTE? Is it a typedef for signed/unsigned char?
    [color=blue]
    > fwrite(&pBuffer , sizeof(pBuffer) , 1 , outdata);[/color]

    Your description is very confusing!

    - 'pBuffer' is of type BYTE
    - 'pBuffer' is written to a file and the file size is then 4KB

    Conclusion: BYTE is a type that occupies 4096bytes. I doubt this,
    especially since you seem to be working unders Windows, where a BYTE is
    defined as (unsigned?) char.

    Is 'pBuffer' of type BYTE* instead? Was the file size 4bytes instead of
    4KB? Is BYTE defined as (unsigned) char? And if 'pBuffer' is a pointer (to
    one or more BYTE's), you actually want to write what the pointer points to,
    correct? If that is the case, then you need to know how many BYTE's you have
    allocated (if it is an array). Once you know that, the call to fwrite looks
    like this:

    fwrite (pBuffer, buffer_size, 1, outdata);

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • Jack Klein

      #3
      Re: Question about fwrite() function

      On 17 Jul 2003 05:50:38 -0700, miahmed67@yahoo .com (seia0106) wrote in
      comp.lang.c++:
      [color=blue]
      > Hello
      >
      > In the course of writing a program. I have to read and write a media
      > file(*.avi/*.mpg/*.wav) to and from memory buffer. I can put the
      > data(from a *.avi file)in buffer successfully but when i try to write
      > data from buffer into a new file, then i can not play that file. A
      > file size of only 4kb is copied and i can not play the file.
      > The size of original file, whose contents were copied into buffer was
      > 996KB.
      > Can anyone please help me solve this problem. Why only 4KB are written
      > into file. I guess may be the problem is in third paramater of
      > fwrite() function 'NumberOfElemen ts', but i am not sure. Here is my
      > simple function
      > pBuffer was declared as global variable of type BYTE
      >
      > void WriteMediaFile( const char file_name[]) {
      > /* Local variable declarations: */
      >
      > FILE *outdata;
      >
      >
      > if ((outdata = fopen(file_name , "w")) == NULL) {[/color]

      Rule #1: open binary files in binary mode ("wb").
      Rule #2: don't ever forget rule #1.
      Rule #3: don't ever forget rule #2.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

      Comment

      • Jack Klein

        #4
        Re: Question about fwrite() function

        On 17 Jul 2003 05:50:38 -0700, miahmed67@yahoo .com (seia0106) wrote in
        comp.lang.c++:
        [color=blue]
        > Hello
        >
        > In the course of writing a program. I have to read and write a media
        > file(*.avi/*.mpg/*.wav) to and from memory buffer. I can put the
        > data(from a *.avi file)in buffer successfully but when i try to write
        > data from buffer into a new file, then i can not play that file. A
        > file size of only 4kb is copied and i can not play the file.
        > The size of original file, whose contents were copied into buffer was
        > 996KB.
        > Can anyone please help me solve this problem. Why only 4KB are written
        > into file. I guess may be the problem is in third paramater of
        > fwrite() function 'NumberOfElemen ts', but i am not sure. Here is my
        > simple function
        > pBuffer was declared as global variable of type BYTE
        >
        > void WriteMediaFile( const char file_name[]) {
        > /* Local variable declarations: */
        >
        > FILE *outdata;
        >
        >
        > if ((outdata = fopen(file_name , "w")) == NULL) {[/color]

        Rule #1: open binary files in binary mode ("wb").
        Rule #2: don't ever forget rule #1.
        Rule #3: don't ever forget rule #2.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

        Comment

        Working...