Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fwrite problems...

Reply
Thread Tools

fwrite problems...

 
 
sumit1680@rediffmail.com
Guest
Posts: n/a
 
      01-06-2006
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???

 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      01-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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/>
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-06-2006
(E-Mail Removed) wrote:
> Hi everyone,
> I am using the below listed code
>
> #include<stdio.h>


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

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


<snip>

> .............................
> 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!
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-06-2006
(E-Mail 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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-07-2006
Flash Gordon wrote:
> (E-Mail Removed) wrote:


> > I am using the below listed code
> >
> > #include<stdio.h>

>
> A bit of horizontal spacing makes it more readable, IMHO
> #include <stdio.h>
>
> > #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.


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

<snip>


--
Nick Keighley

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      01-07-2006
(E-Mail Removed) wrote:
> 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'?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      01-08-2006
(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fwrite() question nescio HTML 0 12-21-2005 10:02 AM
fwrite() and length of string problem nescio HTML 0 12-21-2005 09:50 AM
Newbie: can I rewrite data with fwrite? Zalek Bloom C++ 2 09-09-2003 03:30 AM
Question about fwrite() function seia0106 C++ 3 07-18-2003 07:05 AM
transfer file using recv() and fwrite() michelle C Programming 2 06-26-2003 05:17 PM



Advertisments