fwrite problems...


S

sumit1680

Hi everyone,
I am using the below listed code

#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 on the
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???
 
Ad

Advertisements

C

Chuck F.

Hi everyone,
I am using the below listed code

#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;

}

Interesting. He wastes all sorts of vertical space by using extra
blank lines, but refuses to insert any clarifying blanks in the
actual code lines.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
F

Flash Gordon

Hi everyone,
I am using the below listed code

#include<stdio.h>

A bit of horizontal spacing makes it more readable, IMHO
#include said:
#include<stdlib.h>

#include<string.h>

There is no need to double space the entire file! Just a few blank lines
where it makes it clearer. I've removed most of the rest of the blank
lines so as not to waste vertical space and thus reduce the required
scrolling.
int main()

It is better style to be explicit about the lack of parameters.

int main(void)
{
char *obuf=NULL;
FILE *stream=NULL;
int i=0;

stream=fopen("top","wb");
obuf= (char*)malloc(8192);

You don't need to cast the return value of malloc and, if you had
forgotten to include stdlib.h (which you remembered) it would have meant
the compiler (unless it was that rare breed, a C99 compiler) would not
be *required* to complain at you. Also, it just adds neadless clutter to
the line.
while(i<50)
{
memset(obuf,'a',8192);
fwrite(obuf,sizeof(char),8192,stream);

sizeof(char) is 1 by definition.
printf("single\n");
i++;
}

Wouldn't a for loop have made more sense?

Also, using the magic number 8192 all over the place is not a good idea.
Use #define (or enum) to define a constant and use that, then you
won't miss one when you change your block size.
fclose(stream);
return 0;
}

And my strace is

.............................
As you can see the first write call is of 8192 bytes but later on the
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???

Because it wants to. If you want to know how a specific implementation
works ask on a group dedicated to the implementation. Nothing in the C
language says that fwrite has to output the data in one go. In fact the
C standard defines fwrite in terms of calling fputc once for every
single byte of every single object pointed to by the first parameter, it
is only because the standard allows the implementation to behave "as if"
it had done what is described that you implementation was allowed to
batch up the write in blocks of 4096 bytes!
 
C

Christopher Benson-Manica

(e-mail address removed) wrote:

To add to the comments you've already received: (some snippage)
int main()
{
char *obuf=NULL;
FILE *stream=NULL;

These initial values really don't accomplish anything for you, since
you immediately assign new values to these variables. Many platforms
will warn you that you are "assigning a value that is never used".
int i=0;
stream=fopen("top","wb");
obuf= (char*)malloc(8192);

You should be checking the return value of malloc(), and you should
certainly be checking the return value of fopen(). An ounce of
error detection is worth a pound of core files.
 
N

Nick Keighley

Flash said:
(e-mail address removed) wrote:

A bit of horizontal spacing makes it more readable, IMHO


There is no need to double space the entire file! Just a few blank lines
where it makes it clearer. I've removed most of the rest of the blank
lines so as not to waste vertical space and thus reduce the required
scrolling.

I knew a guy who *always* wrote his code like this. Presumably he'd
worked somewhere where they counted lines...

<snip>
 
J

Joe Wright

Hi everyone,
I am using the below listed code

#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 on the
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???
I have taken the liberty to 'clean' your program a little. Consider..

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NUM 8192
#define TIM 50

int main(void)
{
char *obuf = NULL;
FILE *stream = NULL;
int i, len;
stream = fopen("top", "wb");
obuf = malloc(NUM);
memset(obuf, 'a', NUM);
for (i = 0; i < TIM; ++i) {
len = fwrite(obuf, 1, NUM, stream);
printf("single, %d\n", len);
}
fclose(stream);
return 0;
}

This writes precisely 8192 * 50 (409,600) bytes 'a' to the file "top"
behaving as expected. What is your 'strace'?
 
Ad

Advertisements

T

tmp123

Hi everyone,
I am using the below listed code

#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 on the
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???


Wich one is the block size of the device?

Kind regards.
 

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

Top