Question about fwrite() function

S

seia0106

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 */
 
J

Jakob Bieling

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
 
J

Jack Klein

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
 
J

Jack Klein

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top