By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,161 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,161 IT Pros & Developers. It's quick & easy.

Question about fwrite() function

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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.