Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ReadFile() Problem

ReadFile() Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldenrose9
    wrote on last edited by
    #1

    Content of a.txt

    Hello How are you
    Welcome

    Function used to read the File

    void ReadBytes(__inout BYTE *pBuffer, DWORD dwSize)
    {
    //BYTE *pBuffer = new BYTE[dwSize];
    DWORD dwRead;

    if (hFile != INVALID\_HANDLE\_VALUE)		
    ReadFile(hFile,pBuffer,dwSize,&dwRead,NULL);	
    

    }

    int main()
    {
    myFile mObj;
    BYTE* hSrc = new BYTE[26];
    mObj.ReadBytes(hSrc,26);
    std::cout<

    The output of the above function gives.

    Hello How are you
    Welcomeýýýý««««««««îþ

    Why this extra characters at the end? please help:confused::confused:

    Some Day I Will Prove MySelf :: GOLD

    modified on Tuesday, February 1, 2011 12:44 AM

    D C 2 Replies Last reply
    0
    • G goldenrose9

      Content of a.txt

      Hello How are you
      Welcome

      Function used to read the File

      void ReadBytes(__inout BYTE *pBuffer, DWORD dwSize)
      {
      //BYTE *pBuffer = new BYTE[dwSize];
      DWORD dwRead;

      if (hFile != INVALID\_HANDLE\_VALUE)		
      ReadFile(hFile,pBuffer,dwSize,&dwRead,NULL);	
      

      }

      int main()
      {
      myFile mObj;
      BYTE* hSrc = new BYTE[26];
      mObj.ReadBytes(hSrc,26);
      std::cout<

      The output of the above function gives.

      Hello How are you
      Welcomeýýýý««««««««îþ

      Why this extra characters at the end? please help:confused::confused:

      Some Day I Will Prove MySelf :: GOLD

      modified on Tuesday, February 1, 2011 12:44 AM

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      goldenrose9 wrote:

      Why this extra characters at the end?

      Because cout will output characters until it reaches a \0.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      G 1 Reply Last reply
      0
      • D David Crow

        goldenrose9 wrote:

        Why this extra characters at the end?

        Because cout will output characters until it reaches a \0.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Man who follows car will be exhausted." - Confucius

        G Offline
        G Offline
        goldenrose9
        wrote on last edited by
        #3

        i had checked this with file mapping. In file mapping when i use cout to display the hSrc value it gives the correct output. then why it is displaying extra characters when i am using ReadFile method. Is there any problem in my code. please help:confused:

        Some Day I Will Prove MySelf :: GOLD

        D 1 Reply Last reply
        0
        • G goldenrose9

          i had checked this with file mapping. In file mapping when i use cout to display the hSrc value it gives the correct output. then why it is displaying extra characters when i am using ReadFile method. Is there any problem in my code. please help:confused:

          Some Day I Will Prove MySelf :: GOLD

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          goldenrose9 wrote:

          ...then why it is displaying extra characters when i am using ReadFile method.

          It has nothing to do with ReadFile().

          goldenrose9 wrote:

          Is there any problem in my code.

          Yes, hSrc is not terminated properly.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          G 1 Reply Last reply
          0
          • G goldenrose9

            Content of a.txt

            Hello How are you
            Welcome

            Function used to read the File

            void ReadBytes(__inout BYTE *pBuffer, DWORD dwSize)
            {
            //BYTE *pBuffer = new BYTE[dwSize];
            DWORD dwRead;

            if (hFile != INVALID\_HANDLE\_VALUE)		
            ReadFile(hFile,pBuffer,dwSize,&dwRead,NULL);	
            

            }

            int main()
            {
            myFile mObj;
            BYTE* hSrc = new BYTE[26];
            mObj.ReadBytes(hSrc,26);
            std::cout<

            The output of the above function gives.

            Hello How are you
            Welcomeýýýý««««««««îþ

            Why this extra characters at the end? please help:confused::confused:

            Some Day I Will Prove MySelf :: GOLD

            modified on Tuesday, February 1, 2011 12:44 AM

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            You forgot to terminate the buffer with the null-termination character:

            int main()
            {
            myFile mObj;
            BYTE* hSrc = new BYTE[27];
            mObj.ReadBytes(hSrc,26);
            hSrc[26] = '\0';
            std::cout<
            Cédric Moonen
            Software developer
            Charting control [v3.0]
            OpenGL game tutorial in C++

            G 2 Replies Last reply
            0
            • D David Crow

              goldenrose9 wrote:

              ...then why it is displaying extra characters when i am using ReadFile method.

              It has nothing to do with ReadFile().

              goldenrose9 wrote:

              Is there any problem in my code.

              Yes, hSrc is not terminated properly.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              G Offline
              G Offline
              goldenrose9
              wrote on last edited by
              #6

              then how to solve this.

              Some Day I Will Prove MySelf :: GOLD

              D 1 Reply Last reply
              0
              • G goldenrose9

                then how to solve this.

                Some Day I Will Prove MySelf :: GOLD

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                See here.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                1 Reply Last reply
                0
                • C Cedric Moonen

                  You forgot to terminate the buffer with the null-termination character:

                  int main()
                  {
                  myFile mObj;
                  BYTE* hSrc = new BYTE[27];
                  mObj.ReadBytes(hSrc,26);
                  hSrc[26] = '\0';
                  std::cout<
                  Cédric Moonen
                  Software developer
                  Charting control [v3.0]
                  OpenGL game tutorial in C++

                  G Offline
                  G Offline
                  goldenrose9
                  wrote on last edited by
                  #8

                  thanx :) :thumbsup:

                  Some Day I Will Prove MySelf :: GOLD

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    You forgot to terminate the buffer with the null-termination character:

                    int main()
                    {
                    myFile mObj;
                    BYTE* hSrc = new BYTE[27];
                    mObj.ReadBytes(hSrc,26);
                    hSrc[26] = '\0';
                    std::cout<
                    Cédric Moonen
                    Software developer
                    Charting control [v3.0]
                    OpenGL game tutorial in C++

                    G Offline
                    G Offline
                    goldenrose9
                    wrote on last edited by
                    #9

                    Cedric Moonen wrote:

                    BYTE* hSrc = new BYTE[27];

                    here you have initialized the byte array with +1 of the required value that is 26 and null terminated the byte array at 26. A small doubt. do i have to allocated the byte array +1

                    Some Day I Will Prove MySelf :: GOLD

                    C 1 Reply Last reply
                    0
                    • G goldenrose9

                      Cedric Moonen wrote:

                      BYTE* hSrc = new BYTE[27];

                      here you have initialized the byte array with +1 of the required value that is 26 and null terminated the byte array at 26. A small doubt. do i have to allocated the byte array +1

                      Some Day I Will Prove MySelf :: GOLD

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      Yes, you have to take the termination character into account (it still counts as one char and should be allocated too). Remember that if I allocate an array of 27 characters, the last one is the character at index 26 (since the array is zero-based).

                      Cédric Moonen Software developer
                      Charting control [v3.0] OpenGL game tutorial in C++

                      G 1 Reply Last reply
                      0
                      • C Cedric Moonen

                        Yes, you have to take the termination character into account (it still counts as one char and should be allocated too). Remember that if I allocate an array of 27 characters, the last one is the character at index 26 (since the array is zero-based).

                        Cédric Moonen Software developer
                        Charting control [v3.0] OpenGL game tutorial in C++

                        G Offline
                        G Offline
                        goldenrose9
                        wrote on last edited by
                        #11

                        :) got it. sir, now the program is working fine. but when i pass a bmp or exe file to this function only few bytes from the starting is read into the buffer. Zip Archive

                        PK

                        EXE

                        MZ

                        BMP File

                        BM:

                        am i missing something again.please help..

                        Some Day I Will Prove MySelf :: GOLD

                        C 1 Reply Last reply
                        0
                        • G goldenrose9

                          :) got it. sir, now the program is working fine. but when i pass a bmp or exe file to this function only few bytes from the starting is read into the buffer. Zip Archive

                          PK

                          EXE

                          MZ

                          BMP File

                          BM:

                          am i missing something again.please help..

                          Some Day I Will Prove MySelf :: GOLD

                          C Offline
                          C Offline
                          Cedric Moonen
                          wrote on last edited by
                          #12

                          Are you trying to print those files to the console ? :~ This won't work, since they contain binary information which can perfectly be the character with ASCII code 0 (which will be considered as the end of the string). What are you trying to do here ?

                          Cédric Moonen Software developer
                          Charting control [v3.0] OpenGL game tutorial in C++

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups