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 handling

file handling

Scheduled Pinned Locked Moved C / C++ / MFC
5 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.
  • K Offline
    K Offline
    karmendra_js
    wrote on last edited by
    #1

    i am saving some data to file(CStdio class) in the following format. Address1-Description1 Address2-Description2 Address3-Description3 When ever i want to retrive data i search for the address and get the description. Now when i change the description, it is required that i replace the old description with the new one. One way is to re create the whole file again the only change of new description. This is really inefficient. Can you tell me some nice logic to do this Thanks a lot

    B K 2 Replies Last reply
    0
    • K karmendra_js

      i am saving some data to file(CStdio class) in the following format. Address1-Description1 Address2-Description2 Address3-Description3 When ever i want to retrive data i search for the address and get the description. Now when i change the description, it is required that i replace the old description with the new one. One way is to re create the whole file again the only change of new description. This is really inefficient. Can you tell me some nice logic to do this Thanks a lot

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      Hello, I don't think that it is possible to just 'insert' data in a file. When you write somewhere, data will be overwritten. You have 2 options. You can insert buffers in the file (after an address, description, insert spaces). This way, you can overwrite the description and you have the option to grow until the specified buffer size. The second option you have is to copy all the data after the description that you want to change and write it again after you update the description. If you change the description frequently, I advice option 1. This is much faster. If the changes occurr less ofter and you have lot's of addresses, I suggest the second option. This saves you a lot of space, but it takes more time when you update your file. Hope this helps. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

      K 1 Reply Last reply
      0
      • B Bob Stanneveld

        Hello, I don't think that it is possible to just 'insert' data in a file. When you write somewhere, data will be overwritten. You have 2 options. You can insert buffers in the file (after an address, description, insert spaces). This way, you can overwrite the description and you have the option to grow until the specified buffer size. The second option you have is to copy all the data after the description that you want to change and write it again after you update the description. If you change the description frequently, I advice option 1. This is much faster. If the changes occurr less ofter and you have lot's of addresses, I suggest the second option. This saves you a lot of space, but it takes more time when you update your file. Hope this helps. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

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

        thanks for your suggestions, still i have some things to clarify. I have just 256 addresses so i will go with rewriting the file. But to do this i need to save the rest of the file temporarily, what do you think the data structure i have to use to do this. Thanks again.

        B 1 Reply Last reply
        0
        • K karmendra_js

          i am saving some data to file(CStdio class) in the following format. Address1-Description1 Address2-Description2 Address3-Description3 When ever i want to retrive data i search for the address and get the description. Now when i change the description, it is required that i replace the old description with the new one. One way is to re create the whole file again the only change of new description. This is really inefficient. Can you tell me some nice logic to do this Thanks a lot

          K Offline
          K Offline
          krmed
          wrote on last edited by
          #4

          One idea would be to use a slightly different format for your file...like an .ini file. [Addresses] Address1=Description1 Address2=Description2 Address3=Description3 You could then use GetPrivateProfileString and WritePrivateProfileString to read/change the values. Hope that helps. Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

          1 Reply Last reply
          0
          • K karmendra_js

            thanks for your suggestions, still i have some things to clarify. I have just 256 addresses so i will go with rewriting the file. But to do this i need to save the rest of the file temporarily, what do you think the data structure i have to use to do this. Thanks again.

            B Offline
            B Offline
            Bob Stanneveld
            wrote on last edited by
            #5

            Hello, First, I assume that you are using MFC's CStdioFile class since I don't know of a CStdio class. You can do the following:

            // position after the line you want to change
            // read all the adresses
            CList<CString> StringList;
            CString sLine = "";
            while( YourFileObject.ReadString(sLine) )
            {
            StringList.AddTail(sLine);
            }

            /*
            Change the address you want to change
            Place the file pointer after the changed address
            */
            // write the other address back
            while( !StringList.IsEmpty() )
            {
            YourFileObject.WriteString(StringList.RemoveHead());
            }

            Hope this helps :-D Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

            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