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 question

cfile question

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 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.
  • J Offline
    J Offline
    jafrazee
    wrote on last edited by
    #1

    the following code will not compile but can i load the entire contents of a file into a CString? or is there a better way? int filelen = 0; CString Cbuf; ...... if(rfile.Open(openfile,CFile::modeRead | CFile::shareDenyWrite,NULL)){ filelen = rfile.GetLength(); if(filelen>0){ rfile.Read(Cbuf,filelen); ........ thank you.

    N C T 3 Replies Last reply
    0
    • J jafrazee

      the following code will not compile but can i load the entire contents of a file into a CString? or is there a better way? int filelen = 0; CString Cbuf; ...... if(rfile.Open(openfile,CFile::modeRead | CFile::shareDenyWrite,NULL)){ filelen = rfile.GetLength(); if(filelen>0){ rfile.Read(Cbuf,filelen); ........ thank you.

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      jafrazee wrote: but can i load the entire contents of a file into a CString? or is there a better way? If it's a file of reasonable length, then the answer is you can. But it's not recommended for binary files. But say you have some kinda textual config file which you want to parse. Nothing wrong in reading it into a CString even if it goes to 10-20 KB Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Buy it, read it and admire me :-)

      1 Reply Last reply
      0
      • J jafrazee

        the following code will not compile but can i load the entire contents of a file into a CString? or is there a better way? int filelen = 0; CString Cbuf; ...... if(rfile.Open(openfile,CFile::modeRead | CFile::shareDenyWrite,NULL)){ filelen = rfile.GetLength(); if(filelen>0){ rfile.Read(Cbuf,filelen); ........ thank you.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        if you use the std library, it's very easy to read the entire contents of a file into a string. I show how in my STL series of articles. If CFile won't do the same then it is more pathetic than I thought. Either way, it's generally better to use the std library. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002

        T 1 Reply Last reply
        0
        • J jafrazee

          the following code will not compile but can i load the entire contents of a file into a CString? or is there a better way? int filelen = 0; CString Cbuf; ...... if(rfile.Open(openfile,CFile::modeRead | CFile::shareDenyWrite,NULL)){ filelen = rfile.GetLength(); if(filelen>0){ rfile.Read(Cbuf,filelen); ........ thank you.

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

          LPTSTR psz = Cbuf .GetBuffer (filelen); rfile .Read (psz, filelen); Cbuf .ReleaseBuffer (); Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

          J 1 Reply Last reply
          0
          • C Christian Graus

            if you use the std library, it's very easy to read the entire contents of a file into a string. I show how in my STL series of articles. If CFile won't do the same then it is more pathetic than I thought. Either way, it's generally better to use the std library. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            Bah, there is nothing (for the most part) wrong with CFile. It is just a wrapper for file IO, nothing more. No point in adding 80k of code to you program just to read a simple file. If that is all he needs to do, then CFile will work just fine. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

            C 1 Reply Last reply
            0
            • T Tim Smith

              LPTSTR psz = Cbuf .GetBuffer (filelen); rfile .Read (psz, filelen); Cbuf .ReleaseBuffer (); Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

              J Offline
              J Offline
              jafrazee
              wrote on last edited by
              #6

              thank you. after reading on stl i really did not want to add that much overhead to my program when i am only reading a file that would be about 5k max.

              1 Reply Last reply
              0
              • T Tim Smith

                Bah, there is nothing (for the most part) wrong with CFile. It is just a wrapper for file IO, nothing more. No point in adding 80k of code to you program just to read a simple file. If that is all he needs to do, then CFile will work just fine. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                I guess it depends on your POV. I use STL and iostreams a lot, and a lot within the one project. I could not live without the facilities it offers me, which CFile/CArray does not. The average programmer seems blissfully unaware that such facilities exist, and so will suffer CFile when on a large scale project, where the 80k or so that gets added would be of real benefit to them. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002

                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