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. CFile read

CFile read

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

    Hi, I want to read a .txt file frequently.I wrote this code;

    char *pbuf = new char(file.GetLength())
    file.Read( pbuf,file.GetLength());

    In this way occured an error and program shut down while it is running. But when I wrote

    char pbuf[100];
    file.Read( pbuf,100);

    this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks

    D I T D 4 Replies Last reply
    0
    • I iayd

      Hi, I want to read a .txt file frequently.I wrote this code;

      char *pbuf = new char(file.GetLength())
      file.Read( pbuf,file.GetLength());

      In this way occured an error and program shut down while it is running. But when I wrote

      char pbuf[100];
      file.Read( pbuf,100);

      this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks

      D Offline
      D Offline
      Dominik Reichl
      wrote on last edited by
      #2

      Instead of

      char *pbuf = new char(file.GetLength())

      use

      char *pbuf = new char**[file.GetLength()];**

      Also, don't forget to delete []pbuf, when you've finished using it.


      Too many passwords to remember? Try KeePass Password Safe!

      1 Reply Last reply
      0
      • I iayd

        Hi, I want to read a .txt file frequently.I wrote this code;

        char *pbuf = new char(file.GetLength())
        file.Read( pbuf,file.GetLength());

        In this way occured an error and program shut down while it is running. But when I wrote

        char pbuf[100];
        file.Read( pbuf,100);

        this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        1/

        new char [file.GetLength()];

        2/ You are making a buffer exactly as long as the file - and as it's char buffer, you're likely to be expecting it to be NULL terminated. Which it won't be... Your second example reads *upto* 100 chars, so will probably be less, and as that chunk of 100 bytes probably has a zero or two in it, you get away with it. Make a text file 100 bytes long, and it would likely fail too. 3/ I'd put the size into a variable, if only for debugging purposes. 4/ You could also have some naughty person save a '\0' somewhere in the file, so your string would be messed up... Especially if this program ends up in the wild. The effect could be benign: "Ok, it will look like there was less text. big deal", or critical, depending on your code. Iain.

        Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

        1 Reply Last reply
        0
        • I iayd

          Hi, I want to read a .txt file frequently.I wrote this code;

          char *pbuf = new char(file.GetLength())
          file.Read( pbuf,file.GetLength());

          In this way occured an error and program shut down while it is running. But when I wrote

          char pbuf[100];
          file.Read( pbuf,100);

          this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          as long as you're using MFC, why don't you use CString instead of char* ?

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          R 1 Reply Last reply
          0
          • T toxcct

            as long as you're using MFC, why don't you use CString instead of char* ?

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            Use the rant icon. :-D

            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

            T 1 Reply Last reply
            0
            • R Rajesh R Subramanian

              Use the rant icon. :-D

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              nope, i wasn't ranting, i was just advising, on an interrogative way... :cool:

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              R 1 Reply Last reply
              0
              • T toxcct

                nope, i wasn't ranting, i was just advising, on an interrogative way... :cool:

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Actually yours was a good suggestion and so, could be a rant. :)

                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                1 Reply Last reply
                0
                • I iayd

                  Hi, I want to read a .txt file frequently.I wrote this code;

                  char *pbuf = new char(file.GetLength())
                  file.Read( pbuf,file.GetLength());

                  In this way occured an error and program shut down while it is running. But when I wrote

                  char pbuf[100];
                  file.Read( pbuf,100);

                  this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks

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

                  iayd wrote:

                  char *pbuf = new char(file.GetLength()) file.Read( pbuf,file.GetLength()); In this way occured an error

                  What error? A missing semicolon, perhaps? Did new fail? Did GetLength() fail? Did Read() fail? Be specific.

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  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