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

fwrite problems...

P: n/a
Hi everyone,
I am using the below listed code

The code is

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main()

{

char *obuf=NULL;

FILE *stream=NULL;

int i=0;

stream=fopen("top","wb");

obuf= (char*)malloc(8192);

while(i<50)

{

memset(obuf,'a',8192);

fwrite(obuf,sizeof(char),8192,stream);

printf("single\n");

i++;

}

fclose(stream);

return 0;

}

And my strace is

.................

stat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xf7055000

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 8192) = 8192

fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 221), ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xf7054000

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

..............................

As you can see the first write call is of 8192 bytes but later onthe
subsequent fwrite calls breaks in two calls of write of size 4096 and
4096 bytes. Can anyone explain me the reason behind this behaviour of
fwrite function???

Jan 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
su*******@rediffmail.com wrote:

....

As you can see the first write call is of 8192 bytes but later onthe
subsequent fwrite calls breaks in two calls of write of size 4096 and
4096 bytes. Can anyone explain me the reason behind this behaviour of
fwrite function???

The standard C library is allowed to do this in a manner of it's
choosing. If you want to try and change the buffering arrangements it
makes, look at the setvbuf function, but know that still; more than one
write may be made to the underling OS functions.

It is a good idea to use a reasonable sized buffer with fwrite, since
you want to reduce the number of calls to it to save time. Using fwrite(
buf, 1, 1, stream ) will usually take a long time to write a large file
because of the call overhead.
Jan 7 '06 #2

P: n/a
On 6 Jan 2006 03:47:01 -0800, in comp.lang.c ,
su*******@rediffmail.com wrote:
Hi everyone,
I am using the below listed code (snip code which tries repeatedly to write 8192 bytes to a file)
As you can see the first write call is of 8192 bytes but later onthe
subsequent fwrite calls breaks in two calls of write of size 4096 and
4096 bytes. Can anyone explain me the reason behind this behaviour of
fwrite function???


The library is allowed to perform buffered IO any way it likes, as
long as by the time the next statement is executed, the IO is complete
from your programme's point of view. This allows it to efficiently use
hardware-specific writing routines. Note that it might not even have
written anything to disk at this stage - your disk controller might
have cached all 50 calls to fwrite, and do the whole lot later.
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 7 '06 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

su*******@rediffmail.com wrote:
Hi everyone,
I am using the below listed code

The code is

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main()

{

char *obuf=NULL;

FILE *stream=NULL;

int i=0;

stream=fopen("top","wb");

obuf= (char*)malloc(8192);

while(i<50)

{

memset(obuf,'a',8192);

fwrite(obuf,sizeof(char),8192,stream);

printf("single\n");

i++;

}

fclose(stream);

return 0;

}

And my strace is

................

stat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xf7055000

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 8192) = 8192

fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 221), ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xf7054000

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096

write(1, "single\n", 7) = 7

.............................

As you can see the first write call is of 8192 bytes but later onthe
subsequent fwrite calls breaks in two calls of write of size 4096 and
4096 bytes. Can anyone explain me the reason behind this behaviour of
fwrite function???


Looks to me like you are seeing the platform and implementation-specific
behaviour of the implementation of the C standard library.

Consider this: how big is the buffer that C stream I/O will use to fwrite() to
a file fopen()ed as "wb"? Is it possible that the underlying stream I/O
implementation in the C runtime could make changes to the buffer size when
switching from fwrite() to printf()? Is it possible that the underlying stream
I/O implementation in the C runtime does not make changes to the buffer size
when switching from printf() to fwrite()?

Here's what I see from your strace:

You start off with a 8k buffer filled with the data from the first fwrite()
Immediately after the 1st fwrite(), something allocates a 4K buffer
Next, 7 bytes of data are written from the 1st printf()
Following that, from then on, the fwrite() output is written in 2 chunks of 4k
apiece.

Looks to me like the stdio library changed the buffersize from 8k to 4k
immediately before performing the printf().
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDwGUEagVFX4UWr64RAulCAJ9CyIpTUBC1CtERP443k3 wcvVYplQCgsdaC
0ewVL644vudonhg+j6h/4M4=
=CHLb
-----END PGP SIGNATURE-----
Jan 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.