473,406 Members | 2,336 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,406 software developers and data experts.

fwrite problems...

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

Similar topics

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...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
6
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code #include<stdio.h> #include<stdlib.h> #include<string.h> int main()
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
1
by: Skeets | last post by:
i'm having some php problems. i have the following code on my main page: ------------ $fp = fsockopen("127.0.0.1", 49152, $errno, $errrstr, 30); if (!$fp) { echo "socket error: $errstr...
2
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
1
by: DreAAM | last post by:
Hi, I am having problems with fwrite for large arrays, and would appriciate any help. In a small test program, this doesn't work: fwrite(temp, 4303355904, 1, OUT) The resulting array is only...
19
by: pal | last post by:
Hi all, Could you help me how to use the fwrite( ) in C ++
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.