469,946 Members | 1,931 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

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 'NumberOfElements', 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 */
Jul 19 '05 #1
3 5800
"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
pBuffer was declared as global variable of type BYTE
What kind of type is BYTE? Is it a typedef for signed/unsigned char?
fwrite(&pBuffer, sizeof(pBuffer), 1 , outdata);


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)
Jul 19 '05 #2
On 17 Jul 2003 05:50:38 -0700, mi*******@yahoo.com (seia0106) wrote in
comp.lang.c++:
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 'NumberOfElements', 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) {


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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #3
On 17 Jul 2003 05:50:38 -0700, mi*******@yahoo.com (seia0106) wrote in
comp.lang.c++:
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 'NumberOfElements', 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) {


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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Martin Lucas-Smith | last post: by
3 posts views Thread by Antoine Bloncourt | last post: by
4 posts views Thread by Andrew Kibler | last post: by
30 posts views Thread by Christopher Benson-Manica | last post: by
3 posts views Thread by sumit1680 | last post: by
3 posts views Thread by golden | last post: by
3 posts views Thread by vasu_7799 | last post: by
25 posts views Thread by Abubakar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.