How can I delete a line from a text file in C++?











up vote
1
down vote

favorite












I want to delete a line of my text file without replacing it with " ".
Just as a side note: my file has newlines. So I have more than one line (like a table)



void Data::delete_line(const string& idNr) {
ifstream list;
string readFile, id;
list.open("list.txt", ios::app);

if (list.is_open()) {
while (getline(list, readFile)) {
int pos = readFile.find(';');
id = readFile.substr(0, pos);
if (idNr == id) {
//deleting the line here
}
}
}

}


I found this question but it does not solve my problem:










share|improve this question




















  • 4




    You can't delete a line from an ifstream, at least you'll need to use a std::fstream which is capable to write back to the opened file.
    – πάντα ῥεῖ
    Nov 19 at 19:52






  • 1




    Think of a file like an array. You can't remove the middle element. You have to move everything forward.
    – NathanOliver
    Nov 19 at 19:53










  • Everything after that has to move up, so there is no advantage to not creating a new file
    – stark
    Nov 19 at 19:53










  • @NathanOliver Thats what I want :) IThe other lines have to move after deleting one line
    – IT_Geek_Oz
    Nov 19 at 19:53








  • 1




    If you need a database, use a database, not a plain file.
    – n.m.
    Nov 19 at 19:58















up vote
1
down vote

favorite












I want to delete a line of my text file without replacing it with " ".
Just as a side note: my file has newlines. So I have more than one line (like a table)



void Data::delete_line(const string& idNr) {
ifstream list;
string readFile, id;
list.open("list.txt", ios::app);

if (list.is_open()) {
while (getline(list, readFile)) {
int pos = readFile.find(';');
id = readFile.substr(0, pos);
if (idNr == id) {
//deleting the line here
}
}
}

}


I found this question but it does not solve my problem:










share|improve this question




















  • 4




    You can't delete a line from an ifstream, at least you'll need to use a std::fstream which is capable to write back to the opened file.
    – πάντα ῥεῖ
    Nov 19 at 19:52






  • 1




    Think of a file like an array. You can't remove the middle element. You have to move everything forward.
    – NathanOliver
    Nov 19 at 19:53










  • Everything after that has to move up, so there is no advantage to not creating a new file
    – stark
    Nov 19 at 19:53










  • @NathanOliver Thats what I want :) IThe other lines have to move after deleting one line
    – IT_Geek_Oz
    Nov 19 at 19:53








  • 1




    If you need a database, use a database, not a plain file.
    – n.m.
    Nov 19 at 19:58













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to delete a line of my text file without replacing it with " ".
Just as a side note: my file has newlines. So I have more than one line (like a table)



void Data::delete_line(const string& idNr) {
ifstream list;
string readFile, id;
list.open("list.txt", ios::app);

if (list.is_open()) {
while (getline(list, readFile)) {
int pos = readFile.find(';');
id = readFile.substr(0, pos);
if (idNr == id) {
//deleting the line here
}
}
}

}


I found this question but it does not solve my problem:










share|improve this question















I want to delete a line of my text file without replacing it with " ".
Just as a side note: my file has newlines. So I have more than one line (like a table)



void Data::delete_line(const string& idNr) {
ifstream list;
string readFile, id;
list.open("list.txt", ios::app);

if (list.is_open()) {
while (getline(list, readFile)) {
int pos = readFile.find(';');
id = readFile.substr(0, pos);
if (idNr == id) {
//deleting the line here
}
}
}

}


I found this question but it does not solve my problem:







c++ filestream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 20:26

























asked Nov 19 at 19:48









IT_Geek_Oz

209218




209218








  • 4




    You can't delete a line from an ifstream, at least you'll need to use a std::fstream which is capable to write back to the opened file.
    – πάντα ῥεῖ
    Nov 19 at 19:52






  • 1




    Think of a file like an array. You can't remove the middle element. You have to move everything forward.
    – NathanOliver
    Nov 19 at 19:53










  • Everything after that has to move up, so there is no advantage to not creating a new file
    – stark
    Nov 19 at 19:53










  • @NathanOliver Thats what I want :) IThe other lines have to move after deleting one line
    – IT_Geek_Oz
    Nov 19 at 19:53








  • 1




    If you need a database, use a database, not a plain file.
    – n.m.
    Nov 19 at 19:58














  • 4




    You can't delete a line from an ifstream, at least you'll need to use a std::fstream which is capable to write back to the opened file.
    – πάντα ῥεῖ
    Nov 19 at 19:52






  • 1




    Think of a file like an array. You can't remove the middle element. You have to move everything forward.
    – NathanOliver
    Nov 19 at 19:53










  • Everything after that has to move up, so there is no advantage to not creating a new file
    – stark
    Nov 19 at 19:53










  • @NathanOliver Thats what I want :) IThe other lines have to move after deleting one line
    – IT_Geek_Oz
    Nov 19 at 19:53








  • 1




    If you need a database, use a database, not a plain file.
    – n.m.
    Nov 19 at 19:58








4




4




You can't delete a line from an ifstream, at least you'll need to use a std::fstream which is capable to write back to the opened file.
– πάντα ῥεῖ
Nov 19 at 19:52




You can't delete a line from an ifstream, at least you'll need to use a std::fstream which is capable to write back to the opened file.
– πάντα ῥεῖ
Nov 19 at 19:52




1




1




Think of a file like an array. You can't remove the middle element. You have to move everything forward.
– NathanOliver
Nov 19 at 19:53




Think of a file like an array. You can't remove the middle element. You have to move everything forward.
– NathanOliver
Nov 19 at 19:53












Everything after that has to move up, so there is no advantage to not creating a new file
– stark
Nov 19 at 19:53




Everything after that has to move up, so there is no advantage to not creating a new file
– stark
Nov 19 at 19:53












@NathanOliver Thats what I want :) IThe other lines have to move after deleting one line
– IT_Geek_Oz
Nov 19 at 19:53






@NathanOliver Thats what I want :) IThe other lines have to move after deleting one line
– IT_Geek_Oz
Nov 19 at 19:53






1




1




If you need a database, use a database, not a plain file.
– n.m.
Nov 19 at 19:58




If you need a database, use a database, not a plain file.
– n.m.
Nov 19 at 19:58












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Maybe you can just create a new file and put your data into the new file. Like this:



void Data::delete_line(const string& idNr) {
ifstream list;
ofstream outFile("newList.txt");
string readFile, id;

list.open("list.txt", ios::app);

if (list.is_open()) {
while (getline(list, readFile)) {
int pos = readFile.find(';');
id = readFile.substr(0, pos);
if (idNr != id) {
outFile << readFile;
}
}
}
list.close();
outFile.close();

remove("list.txt");
rename("newList.txt", "list.txt");
}


At the end you just remove your old file and rename the new file with the name of the old file. I hope this will solve your problem.






share|improve this answer




























    up vote
    0
    down vote













    Without creating a new file, Its impossible to remove a line in the middle of file. Unless you replace it in place.



    So in this state, just you have to use of bellow method :



    std::ostream::seekp


    Notice this example :



    // position in output stream
    #include <fstream> // std::ofstream

    int main () {

    std::ofstream outfile;
    outfile.open ("test.txt");

    outfile.write ("This is an apple",16);
    long pos = outfile.tellp();
    outfile.seekp (pos-7);
    outfile.write (" sam",4);

    outfile.close();

    return 0;
    }


    In this example, tellp is used to get the position in the stream after the writing operation. The pointer is then moved back 7 characters to modify the file at that position, so the final content of the file is:



    This is a sample






    share|improve this answer























    • The sample is not working
      – IT_Geek_Oz
      Nov 19 at 20:17










    • @MartinYork Fixed...
      – Mohammadreza Panahi
      Nov 19 at 20:32











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381654%2fhow-can-i-delete-a-line-from-a-text-file-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Maybe you can just create a new file and put your data into the new file. Like this:



    void Data::delete_line(const string& idNr) {
    ifstream list;
    ofstream outFile("newList.txt");
    string readFile, id;

    list.open("list.txt", ios::app);

    if (list.is_open()) {
    while (getline(list, readFile)) {
    int pos = readFile.find(';');
    id = readFile.substr(0, pos);
    if (idNr != id) {
    outFile << readFile;
    }
    }
    }
    list.close();
    outFile.close();

    remove("list.txt");
    rename("newList.txt", "list.txt");
    }


    At the end you just remove your old file and rename the new file with the name of the old file. I hope this will solve your problem.






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Maybe you can just create a new file and put your data into the new file. Like this:



      void Data::delete_line(const string& idNr) {
      ifstream list;
      ofstream outFile("newList.txt");
      string readFile, id;

      list.open("list.txt", ios::app);

      if (list.is_open()) {
      while (getline(list, readFile)) {
      int pos = readFile.find(';');
      id = readFile.substr(0, pos);
      if (idNr != id) {
      outFile << readFile;
      }
      }
      }
      list.close();
      outFile.close();

      remove("list.txt");
      rename("newList.txt", "list.txt");
      }


      At the end you just remove your old file and rename the new file with the name of the old file. I hope this will solve your problem.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Maybe you can just create a new file and put your data into the new file. Like this:



        void Data::delete_line(const string& idNr) {
        ifstream list;
        ofstream outFile("newList.txt");
        string readFile, id;

        list.open("list.txt", ios::app);

        if (list.is_open()) {
        while (getline(list, readFile)) {
        int pos = readFile.find(';');
        id = readFile.substr(0, pos);
        if (idNr != id) {
        outFile << readFile;
        }
        }
        }
        list.close();
        outFile.close();

        remove("list.txt");
        rename("newList.txt", "list.txt");
        }


        At the end you just remove your old file and rename the new file with the name of the old file. I hope this will solve your problem.






        share|improve this answer












        Maybe you can just create a new file and put your data into the new file. Like this:



        void Data::delete_line(const string& idNr) {
        ifstream list;
        ofstream outFile("newList.txt");
        string readFile, id;

        list.open("list.txt", ios::app);

        if (list.is_open()) {
        while (getline(list, readFile)) {
        int pos = readFile.find(';');
        id = readFile.substr(0, pos);
        if (idNr != id) {
        outFile << readFile;
        }
        }
        }
        list.close();
        outFile.close();

        remove("list.txt");
        rename("newList.txt", "list.txt");
        }


        At the end you just remove your old file and rename the new file with the name of the old file. I hope this will solve your problem.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 20:34









        gmz

        395




        395
























            up vote
            0
            down vote













            Without creating a new file, Its impossible to remove a line in the middle of file. Unless you replace it in place.



            So in this state, just you have to use of bellow method :



            std::ostream::seekp


            Notice this example :



            // position in output stream
            #include <fstream> // std::ofstream

            int main () {

            std::ofstream outfile;
            outfile.open ("test.txt");

            outfile.write ("This is an apple",16);
            long pos = outfile.tellp();
            outfile.seekp (pos-7);
            outfile.write (" sam",4);

            outfile.close();

            return 0;
            }


            In this example, tellp is used to get the position in the stream after the writing operation. The pointer is then moved back 7 characters to modify the file at that position, so the final content of the file is:



            This is a sample






            share|improve this answer























            • The sample is not working
              – IT_Geek_Oz
              Nov 19 at 20:17










            • @MartinYork Fixed...
              – Mohammadreza Panahi
              Nov 19 at 20:32















            up vote
            0
            down vote













            Without creating a new file, Its impossible to remove a line in the middle of file. Unless you replace it in place.



            So in this state, just you have to use of bellow method :



            std::ostream::seekp


            Notice this example :



            // position in output stream
            #include <fstream> // std::ofstream

            int main () {

            std::ofstream outfile;
            outfile.open ("test.txt");

            outfile.write ("This is an apple",16);
            long pos = outfile.tellp();
            outfile.seekp (pos-7);
            outfile.write (" sam",4);

            outfile.close();

            return 0;
            }


            In this example, tellp is used to get the position in the stream after the writing operation. The pointer is then moved back 7 characters to modify the file at that position, so the final content of the file is:



            This is a sample






            share|improve this answer























            • The sample is not working
              – IT_Geek_Oz
              Nov 19 at 20:17










            • @MartinYork Fixed...
              – Mohammadreza Panahi
              Nov 19 at 20:32













            up vote
            0
            down vote










            up vote
            0
            down vote









            Without creating a new file, Its impossible to remove a line in the middle of file. Unless you replace it in place.



            So in this state, just you have to use of bellow method :



            std::ostream::seekp


            Notice this example :



            // position in output stream
            #include <fstream> // std::ofstream

            int main () {

            std::ofstream outfile;
            outfile.open ("test.txt");

            outfile.write ("This is an apple",16);
            long pos = outfile.tellp();
            outfile.seekp (pos-7);
            outfile.write (" sam",4);

            outfile.close();

            return 0;
            }


            In this example, tellp is used to get the position in the stream after the writing operation. The pointer is then moved back 7 characters to modify the file at that position, so the final content of the file is:



            This is a sample






            share|improve this answer














            Without creating a new file, Its impossible to remove a line in the middle of file. Unless you replace it in place.



            So in this state, just you have to use of bellow method :



            std::ostream::seekp


            Notice this example :



            // position in output stream
            #include <fstream> // std::ofstream

            int main () {

            std::ofstream outfile;
            outfile.open ("test.txt");

            outfile.write ("This is an apple",16);
            long pos = outfile.tellp();
            outfile.seekp (pos-7);
            outfile.write (" sam",4);

            outfile.close();

            return 0;
            }


            In this example, tellp is used to get the position in the stream after the writing operation. The pointer is then moved back 7 characters to modify the file at that position, so the final content of the file is:



            This is a sample







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 20:32

























            answered Nov 19 at 20:08









            Mohammadreza Panahi

            2,52221432




            2,52221432












            • The sample is not working
              – IT_Geek_Oz
              Nov 19 at 20:17










            • @MartinYork Fixed...
              – Mohammadreza Panahi
              Nov 19 at 20:32


















            • The sample is not working
              – IT_Geek_Oz
              Nov 19 at 20:17










            • @MartinYork Fixed...
              – Mohammadreza Panahi
              Nov 19 at 20:32
















            The sample is not working
            – IT_Geek_Oz
            Nov 19 at 20:17




            The sample is not working
            – IT_Geek_Oz
            Nov 19 at 20:17












            @MartinYork Fixed...
            – Mohammadreza Panahi
            Nov 19 at 20:32




            @MartinYork Fixed...
            – Mohammadreza Panahi
            Nov 19 at 20:32


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381654%2fhow-can-i-delete-a-line-from-a-text-file-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown