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. Deleting a line in a text file

Deleting a line in a text file

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
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.
  • A Offline
    A Offline
    Azghar Hussain
    wrote on last edited by
    #1

    Please help me!!! Can any one tell me how to delete a line in text file. Please tell me how to do it in C (not MFC, C++) Thanks... If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

    C 1 Reply Last reply
    0
    • A Azghar Hussain

      Please help me!!! Can any one tell me how to delete a line in text file. Please tell me how to do it in C (not MFC, C++) Thanks... If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

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

      You can't. What you have to do is read the file in memory, erase the line and write the complete file back. An alternative is to open a temporary file, write line by line in it (except for the line you want to remove), delete your original file and then rename your new file.


      Cédric Moonen Software developer
      Charting control

      A 1 Reply Last reply
      0
      • C Cedric Moonen

        You can't. What you have to do is read the file in memory, erase the line and write the complete file back. An alternative is to open a temporary file, write line by line in it (except for the line you want to remove), delete your original file and then rename your new file.


        Cédric Moonen Software developer
        Charting control

        A Offline
        A Offline
        Azghar Hussain
        wrote on last edited by
        #3

        I cannot take big data inside memory because I may have 1 Lakh records to be written to the file. I cannot also use temp file because I may to exchange data from original file to temp file alteast 50,000 times. If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

        T C L 3 Replies Last reply
        0
        • A Azghar Hussain

          I cannot take big data inside memory because I may have 1 Lakh records to be written to the file. I cannot also use temp file because I may to exchange data from original file to temp file alteast 50,000 times. If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

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

          you 'll have to BTW, because there's no other way to do it


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          1 Reply Last reply
          0
          • A Azghar Hussain

            I cannot take big data inside memory because I may have 1 Lakh records to be written to the file. I cannot also use temp file because I may to exchange data from original file to temp file alteast 50,000 times. If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

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

            There is no way of doing that. Mmmh, I think there is problem with your design: if you have to update files so many times, I don't think this is the right choice. Can't you use a database for that ? What is the purpose of this file exactly ?


            Cédric Moonen Software developer
            Charting control

            A 1 Reply Last reply
            0
            • C Cedric Moonen

              There is no way of doing that. Mmmh, I think there is problem with your design: if you have to update files so many times, I don't think this is the right choice. Can't you use a database for that ? What is the purpose of this file exactly ?


              Cédric Moonen Software developer
              Charting control

              A Offline
              A Offline
              Azghar Hussain
              wrote on last edited by
              #6

              Requirement is we have to query data from database and modify the recordsets and write the data into the text file. If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

              1 Reply Last reply
              0
              • A Azghar Hussain

                I cannot take big data inside memory because I may have 1 Lakh records to be written to the file. I cannot also use temp file because I may to exchange data from original file to temp file alteast 50,000 times. If you have faith in the cause and the means and in God, the hot Sun will be cool for you.

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                An optimization is to write the remaining file from the point of the deleted data rather than the entire file.

                Bahadur - yar - Jung wrote:

                I cannot take big data inside memory because I may have 1 Lakh records to be written to the file.

                Use buffered IO

                "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
                Christian Graus in the C# forum

                led mike

                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