473,414 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 5974
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
3
by: Antoine Bloncourt | last post by:
Hello everybody Sorry to bother you but I have a problem writing datas into a file ... I want to make a backup of my MySQL database and put the result into a ..sql file. To do this, I use...
4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
30
by: Christopher Benson-Manica | last post by:
Given the following: char s="Hello, world\n!"; Are all the following guaranteed to produce the same output? printf( "%s", s ); fprintf( stdout, "%s", s ); fwrite( s, sizeof(char),...
3
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
3
by: golden | last post by:
Hello, I am going to ask a question regarding write and lseek. I will provide code at the end of this, but first some background. I am trying to identify the cause of some latency in...
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
3
by: vasu_7799 | last post by:
Hi, I have 2 structure. struct a { char cn; char tl; char roup; char rc; char adj;
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.