Unexpected Output when reading a files using read()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sanchit

    Unexpected Output when reading a files using read()

    My program is

    #include <fcntl.h>
    #include <unistd.h>
    int main(void)
    {
    int fd;
    ssize_t nread;
    char buf[100];
    /*open file for reading */
    fd = open("marks.dat ", O_RDONLY);
    /* read the data */
    nread = read(fd, buf, 1024);
    /*close the file */

    printf("%s\n\n" ,buf);
    close(fd);
    }

    And my marks.dat is
    123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d(HERE FILE
    ENDS)

    Output is
    123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
    Ȭ¿^äì·9 ÷·ž–ž± ¬Â¿XƒôÏú ·Å¾Â–ر¬¿ )Â…


    I am unable to understand why these special symbols are coming
  • Morris Dovey

    #2
    Re: Unexpected Output when reading a files using read()

    Sanchit wrote:
    >
    My program is
    >
    #include <fcntl.h>
    #include <unistd.h>
    int main(void)
    {
    int fd;
    ssize_t nread;
    char buf[100];
    /*open file for reading */
    fd = open("marks.dat ", O_RDONLY);
    /* read the data */
    nread = read(fd, buf, 1024);
    /*close the file */
    >
    printf("%s\n\n" ,buf);
    close(fd);
    }
    >
    And my marks.dat is
    123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d(HERE FILE
    ENDS)
    >
    Output is
    123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
    Ã^¬¿^äì·9 ÷·žÂ-ž±¬¿XÂf ôÃ?ú·žÂ-Ã~±¬¿)Â?
    >
    I am unable to understand why these special symbols are coming
    The %s specification in printf() caused printing of the data at
    buf until a '\0' was found. What do you suppose would have
    happened if no terminator at all had been found?

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA

    Comment

    • maxim.pgv

      #3
      Re: Unexpected Output when reading a files using read()

      On 21 мар, 12:49, Sanchit <sanchitgupt... @gmail.comwrote :
      Output is
      123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
      Ȭ¿^äì·9 ÷·ž– ž±¬¿Xƒ ôÏú·ž– ر¬¿)…
      >
      I am unable to understand why these special symbols are coming
      Try to clear your buffer:

      memset( buf, 0, sizeof(buf) );

      Comment

      • Ben Bacarisse

        #4
        Re: Unexpected Output when reading a files using read()

        Richard Heathfield <rjh@see.sig.in validwrites:
        Sanchit said:
        >
        >My program is
        >>
        >#include <fcntl.h>
        >#include <unistd.h>
        <snip>
        Alternative solution: after the read() call, test nread to see whether the
        call worked. If so, and if nread is < 100, you can just do this:
        >
        buf[nread] = {0};
        Typo: you meant

        buf[nread] = 0;

        (or maybe = '\0').

        To the OP: in addition to what has been said, you must include stdio.h
        to make the use of printf well-defined.

        --
        Ben.

        Comment

        • Richard Heathfield

          #5
          Re: Unexpected Output when reading a files using read()

          Ben Bacarisse said:
          Richard Heathfield <rjh@see.sig.in validwrites:
          >
          >[...] if nread is < 100, you can just do this:
          >>
          > buf[nread] = {0};
          >
          Typo: you meant
          >
          buf[nread] = 0;
          >
          (or maybe = '\0').
          Ta.
          To the OP: in addition to what has been said, you must include stdio.h
          to make the use of printf well-defined.
          Ooooh, ta twicely.

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • jacob navia

            #6
            Re: Unexpected Output when reading a files using read()

            Richard Heathfield wrote:
            Ben Bacarisse said:
            >
            >Richard Heathfield <rjh@see.sig.in validwrites:
            >>
            >>[...] if nread is < 100, you can just do this:
            >>>
            >> buf[nread] = {0};
            >Typo: you meant
            >>
            > buf[nread] = 0;
            >>
            >(or maybe = '\0').
            >
            Ta.
            >
            >To the OP: in addition to what has been said, you must include stdio.h
            >to make the use of printf well-defined.
            >
            Ooooh, ta twicely.
            >
            Yeah. Always so fast to see a missing
            'L'
            when writing an integer constant, then blaming people for that.

            Note: You should read your code before you post!


            --
            jacob navia
            jacob at jacob point remcomp point fr
            logiciels/informatique

            Comment

            • Richard Heathfield

              #7
              Re: Unexpected Output when reading a files using read()

              jacob navia said:
              Richard Heathfield wrote:
              >Ben Bacarisse said:
              >>
              >>Richard Heathfield <rjh@see.sig.in validwrites:
              >>>
              >>>[...] if nread is < 100, you can just do this:
              >>>>
              >>> buf[nread] = {0};
              >>Typo: you meant
              >>>
              >> buf[nread] = 0;
              >>>
              >>(or maybe = '\0').
              >>
              >Ta.
              >>
              >>To the OP: in addition to what has been said, you must include stdio.h
              >>to make the use of printf well-defined.
              >>
              >Ooooh, ta twicely.
              >>
              >
              Yeah. Always so fast to see a missing
              'L'
              when writing an integer constant, then blaming people for that.
              I don't know what you're talking about. I haven't blamed anyone for missing
              an L suffix.
              Note: You should read your code before you post!
              Well, I do - I read it while I'm writing it. But I don't claim to be
              perfect, and I do make mistakes. When that happens, I fess up. It is a
              strategy that I recommend highly.

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Keith Thompson

                #8
                Re: Unexpected Output when reading a files using read()

                Sanchit <sanchitgupta.1 @gmail.comwrite s:
                My program is
                >
                #include <fcntl.h>
                #include <unistd.h>
                int main(void)
                {
                int fd;
                ssize_t nread;
                char buf[100];
                /*open file for reading */
                fd = open("marks.dat ", O_RDONLY);
                /* read the data */
                nread = read(fd, buf, 1024);
                /*close the file */
                >
                printf("%s\n\n" ,buf);
                close(fd);
                }
                [snip]

                In addition to the other comments (attempting to read 1024 bytes into
                a 100-byte buffer, printing with "%s" something that isn't a string,
                and the missing "#include <stdio.h>"), you might consider using the
                standard C functions (fopen, fread, fclose) rather than the
                POSIX-specific ones you're using (open, read, close). There are times
                when the POSIX functions have advantages over the corresponding
                standard C functions, but I don't see any indication that this is one
                of those times.

                --
                Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                Working...