fwrite problems...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sumit1680@rediffmail.com

    #1

    fwrite problems...

    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("t op","wb");

    obuf= (char*)malloc(8 192);

    while(i<50)

    {

    memset(obuf,'a' ,8192);

    fwrite(obuf,siz eof(char),8192, stream);

    printf("single\ n");

    i++;

    }

    fclose(stream);

    return 0;

    }





    And my strace is

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

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

    mmap2(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
    0) = 0xf7055000

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 8192) = 8192

    fstat64(1, {st_mode=S_IFCH R|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, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

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

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

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

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

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

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

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

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096

    write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 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???

  • Chuck F.

    #2
    Re: fwrite problems...

    sumit1680@redif fmail.com wrote:[color=blue]
    > 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("t op","wb");
    >
    > obuf= (char*)malloc(8 192);
    >
    > while(i<50)
    >
    > {
    >
    > memset(obuf,'a' ,8192);
    >
    > fwrite(obuf,siz eof(char),8192, stream);
    >
    > printf("single\ n");
    >
    > i++;
    >
    > }
    >
    > fclose(stream);
    >
    > return 0;
    >
    > }[/color]

    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.c om, 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/>

    Comment

    • Flash Gordon

      #3
      Re: fwrite problems...

      sumit1680@redif fmail.com wrote:[color=blue]
      > Hi everyone,
      > I am using the below listed code
      >
      > #include<stdio. h>[/color]

      A bit of horizontal spacing makes it more readable, IMHO
      #include <stdio.h>
      [color=blue]
      > #include<stdlib .h>
      >
      > #include<string .h>[/color]

      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.
      [color=blue]
      > int main()[/color]

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

      int main(void)
      [color=blue]
      > {
      > char *obuf=NULL;
      > FILE *stream=NULL;
      > int i=0;
      >
      > stream=fopen("t op","wb");
      > obuf= (char*)malloc(8 192);[/color]

      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.
      [color=blue]
      > while(i<50)
      > {
      > memset(obuf,'a' ,8192);
      > fwrite(obuf,siz eof(char),8192, stream);[/color]

      sizeof(char) is 1 by definition.
      [color=blue]
      > printf("single\ n");
      > i++;
      > }[/color]

      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.
      [color=blue]
      > fclose(stream);
      > return 0;
      > }
      >
      > And my strace is[/color]

      <snip>
      [color=blue]
      > ............... ..............
      > 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???[/color]

      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.

      Comment

      • Christopher Benson-Manica

        #4
        Re: fwrite problems...

        sumit1680@redif fmail.com wrote:

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

        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".
        [color=blue]
        > int i=0;
        > stream=fopen("t op","wb");
        > obuf= (char*)malloc(8 192);[/color]

        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)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • Joe Wright

          #5
          Re: fwrite problems...

          sumit1680@redif fmail.com wrote:[color=blue]
          > 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("t op","wb");
          >
          > obuf= (char*)malloc(8 192);
          >
          > while(i<50)
          >
          > {
          >
          > memset(obuf,'a' ,8192);
          >
          > fwrite(obuf,siz eof(char),8192, stream);
          >
          > printf("single\ n");
          >
          > i++;
          >
          > }
          >
          > fclose(stream);
          >
          > return 0;
          >
          > }
          >
          >
          >
          >
          >
          > And my strace is
          >
          > ............... .
          >
          > stat64(3, {st_mode=S_IFRE G|0644, st_size=0, ...}) = 0
          >
          > mmap2(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
          > 0) = 0xf7055000
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 8192) = 8192
          >
          > fstat64(1, {st_mode=S_IFCH R|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, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(1, "single\n", 7) = 7
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(1, "single\n", 7) = 7
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(1, "single\n", 7) = 7
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(1, "single\n", 7) = 7
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
          >
          > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 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???
          >[/color]
          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 ---

          Comment

          • Nick Keighley

            #6
            Re: fwrite problems...

            Flash Gordon wrote:[color=blue]
            > sumit1680@redif fmail.com wrote:[/color]
            [color=blue][color=green]
            > > I am using the below listed code
            > >
            > > #include<stdio. h>[/color]
            >
            > A bit of horizontal spacing makes it more readable, IMHO
            > #include <stdio.h>
            >[color=green]
            > > #include<stdlib .h>
            > >
            > > #include<string .h>[/color]
            >
            > 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.[/color]

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

            <snip>


            --
            Nick Keighley

            Comment

            • tmp123

              #7
              Re: fwrite problems...

              sumit1680@redif fmail.com wrote:[color=blue]
              > 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("t op","wb");
              >
              > obuf= (char*)malloc(8 192);
              >
              > while(i<50)
              >
              > {
              >
              > memset(obuf,'a' ,8192);
              >
              > fwrite(obuf,siz eof(char),8192, stream);
              >
              > printf("single\ n");
              >
              > i++;
              >
              > }
              >
              > fclose(stream);
              >
              > return 0;
              >
              > }
              >
              >
              >
              >
              >
              > And my strace is
              >
              > ............... .
              >
              > stat64(3, {st_mode=S_IFRE G|0644, st_size=0, ...}) = 0
              >
              > mmap2(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
              > 0) = 0xf7055000
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 8192) = 8192
              >
              > fstat64(1, {st_mode=S_IFCH R|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, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(1, "single\n", 7) = 7
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(1, "single\n", 7) = 7
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(1, "single\n", 7) = 7
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(1, "single\n", 7) = 7
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 4096) = 4096
              >
              > write(3, "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa"..., 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???[/color]


              Wich one is the block size of the device?

              Kind regards.

              Comment

              Working...