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. Problem with reading a file

Problem with reading a file

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
8 Posts 7 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.
  • R Offline
    R Offline
    Rakesh5
    wrote on last edited by
    #1

    Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

    _ C S M D 6 Replies Last reply
    0
    • R Rakesh5

      Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Does CStdioFile have open and read methods? The length parameter to any read function is the amount of data to be read from the file. I believe there is some mistake here. The first parameter to the read function must be a buffer containing some memory and not just a pointer. Look at the example in the documentation of CFile::Read[^].

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • R Rakesh5

        Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

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

        You never allocate preadbuf, so I'm really amazed that your code doesn't crash here...

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

        1 Reply Last reply
        0
        • R Rakesh5

          Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

          S Offline
          S Offline
          shuzh
          wrote on last edited by
          #4

          这个东西 貌似很好弄袄

          M 1 Reply Last reply
          0
          • S shuzh

            这个东西 貌似很好弄袄

            M Offline
            M Offline
            Michael Schubert
            wrote on last edited by
            #5

            shuzh wrote:

            这个东西 貌似很好弄袄

            Exactly.

            1 Reply Last reply
            0
            • R Rakesh5

              Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

              M Offline
              M Offline
              Michael Schubert
              wrote on last edited by
              #6

              First you should make up your mind if you want to read the file line by line or read the entire file into a buffer. In case of the former option use CStdioFile::ReadString() method, for the latter use CFile::Read().

              1 Reply Last reply
              0
              • R Rakesh5

                Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

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

                Rakesh5 wrote:

                if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength());

                This will not even compile. If you want to know why a particular piece of code does not work, at least show the actual code.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "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

                1 Reply Last reply
                0
                • R Rakesh5

                  Hi all, I want to read the content of the file.(say .txt) I am using Read method...But Its not reading the entire contents present in the file rather it reads around 50% of the file. ------------------------- Code Snippet: CStdioFile docfile; WCHAR* preadbuf; if(docfile.open(....)) docfile.read(preadbuf,docfile.getlength()); ------------ Please tell me how to solve this problem. please let me know if you have any queries.. Thanks, Rakesh.

                  K Offline
                  K Offline
                  kakan
                  wrote on last edited by
                  #8

                  Just a thought. But here goes: You seems to read the file as Unicode, that way every character consists of two bytes in the file. If docfile.getlength() reports the file size in bytes, then you get your 50%. An example, suppose the file is 50 bytes. docfile.getlength() returns 50. But you can read 25 Unicode characters from it. Makes sense?

                  Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                  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