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. File read problem

File read problem

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

    I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,

    priyank

    K K D 3 Replies Last reply
    0
    • P pri_skit

      I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,

      priyank

      K Offline
      K Offline
      karle
      wrote on last edited by
      #2

      you better use CStdioFile class which is derived from CFile. It has a ReadString and a WriteString method.

      1 Reply Last reply
      0
      • P pri_skit

        I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,

        priyank

        K Offline
        K Offline
        KEL3
        wrote on last edited by
        #3

        Try the following to write: CString StringToWrite; StringToWrite="ABC";//e.g. f.Write (StringToWrite.GetBuffer(20),StringToWrite.GetLength()+1); Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); str+=B; if(B=='\0')break; } Not the best way to do it, but if it work U R OK!

        kostas KEL

        D 1 Reply Last reply
        0
        • K KEL3

          Try the following to write: CString StringToWrite; StringToWrite="ABC";//e.g. f.Write (StringToWrite.GetBuffer(20),StringToWrite.GetLength()+1); Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); str+=B; if(B=='\0')break; } Not the best way to do it, but if it work U R OK!

          kostas KEL

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

          KEL3 wrote:

          f.Write (StringToWrite.GetBuffer(20),StringToWrite.GetLength()+1);

          Why are you unnecessarily calling GetBuffer()?


          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

          1 Reply Last reply
          0
          • P pri_skit

            I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,

            priyank

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

            pri_skit wrote:

            f.Write (&FileName,FileName.GetLength());//FileName is CString Object

            You might want to check the contents of the file you are writing to, as you are writing the address of the CString object to the file.

            pri_skit wrote:

            //But this method don't work.

            Why are you bothering with a void pointer?


            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

            K 1 Reply Last reply
            0
            • D David Crow

              pri_skit wrote:

              f.Write (&FileName,FileName.GetLength());//FileName is CString Object

              You might want to check the contents of the file you are writing to, as you are writing the address of the CString object to the file.

              pri_skit wrote:

              //But this method don't work.

              Why are you bothering with a void pointer?


              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

              K Offline
              K Offline
              KEL3
              wrote on last edited by
              #6

              DavidCrow wrote: Why are you unnecessarily calling GetBuffer()? If you write: f.Write (&FileName,FileName.GetLength()); you are about to write the whole object into the file (including private variables etc...) and that is generally BAD. In fact the correct code for that would be: f.Write (&FileName,sizeof(FileName)); since the previous code would probably crash your app if the string buffer (allocated on the heap) is longer than the size of the object. =============================================== A little correction: Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); if(B=='\0')break; // Before "str+=B;" to avoid adding two '\0' at the end. str+=B; }

              kostas KEL

              D 1 Reply Last reply
              0
              • K KEL3

                DavidCrow wrote: Why are you unnecessarily calling GetBuffer()? If you write: f.Write (&FileName,FileName.GetLength()); you are about to write the whole object into the file (including private variables etc...) and that is generally BAD. In fact the correct code for that would be: f.Write (&FileName,sizeof(FileName)); since the previous code would probably crash your app if the string buffer (allocated on the heap) is longer than the size of the object. =============================================== A little correction: Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); if(B=='\0')break; // Before "str+=B;" to avoid adding two '\0' at the end. str+=B; }

                kostas KEL

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

                KEL3 wrote:

                In fact the correct code for that would be: f.Write (&FileName,sizeof(FileName));

                Wrong. This would still write the address of FileName to the file. Why is it so hard to code something like:

                f.Write(FileName, FileName.GetLength());


                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

                K 1 Reply Last reply
                0
                • D David Crow

                  KEL3 wrote:

                  In fact the correct code for that would be: f.Write (&FileName,sizeof(FileName));

                  Wrong. This would still write the address of FileName to the file. Why is it so hard to code something like:

                  f.Write(FileName, FileName.GetLength());


                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

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

                  Yes, sorry. I meant that, f.Write (&FileName,sizeof(FileName)); would be the correct code to save the object in the file, not the string that it has allocated. I would be more clear in future! But the code I posted above: f.Write(FileName.GetBuffer(20), FileName.GetLength()+1); works. I have done it before (that's why I decided to answer, despite the fact I'm not an expert). Your piece of code does the same with less code! I just didn't have in mind that CString could be so easily type-casted into LPCSTR and then to void*. But you must also use the +1, so that the '\0' character be written. This is needed if you don't know the size of the string from the beginning. No mean to argue, you are right. Thanks for helping me in the past too. Remember this: http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=2117813&df=100&fr=15724.5#xx2117813xx[^]

                  kostas KEL

                  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