fwrite problems...

Discussion in 'C Programming' started by sumit1680@rediffmail.com, Jan 6, 2006.

  1. Guest

    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???
    , Jan 6, 2006
    #1
    1. Advertising

  2. Chuck F. Guest

    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/>
    Chuck F., Jan 6, 2006
    #2
    1. Advertising

  3. Flash Gordon Guest

    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.
    Flash Gordon, Jan 6, 2006
    #3
  4. 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.
    Christopher Benson-Manica, Jan 6, 2006
    #4
  5. Flash Gordon wrote:
    > 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
    Nick Keighley, Jan 7, 2006
    #5
  6. Joe Wright Guest

    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 ---
    Joe Wright, Jan 7, 2006
    #6
  7. tmp123 Guest

    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.
    tmp123, Jan 8, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nescio
    Replies:
    0
    Views:
    447
    nescio
    Dec 21, 2005
  2. nescio

    fwrite() question

    nescio, Dec 21, 2005, in forum: HTML
    Replies:
    0
    Views:
    359
    nescio
    Dec 21, 2005
  3. seia0106
    Replies:
    3
    Views:
    3,113
    Jack Klein
    Jul 18, 2003
  4. Zalek Bloom
    Replies:
    2
    Views:
    736
    llewelly
    Sep 9, 2003
  5. fwrite problems...

    , Jan 6, 2006, in forum: C Programming
    Replies:
    3
    Views:
    1,497
    Lew Pitcher
    Jan 8, 2006
Loading...

Share This Page