简体   繁体   中英

Delete last line from text file using fstream

I want to remove the last line ("%12345") from some text that is stored in my data.txt file

Can anyone tell me how can I achieve what I want. Sorry I don't have any code yet. If you gave me some examples I really appreciate it.


data.txt :

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown prindfdtersd took a was galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
%12345

#include <iostream.h>
#include <fstream.h>
main()
{
     char search[500];
     ifstream inFile;
     ofstream outFile;
     inFile.open("data.txt");

     while(!inFile.eof())
     {
          inFile.getline(search,500);
          outFile.open("data.txt");
          outFile.seekp(0,ios::end); //Seek to the end of text file
          //I don't know what to do here to delete last line????            
          outFile.close();
     }
     inFile.close();
     return 0;
}

If you want the code to be 100% portable, about all you can do is read all the text, and copy it line-by-line, leaving out the last line. If the real file is as small as the example in the question, you might as well just go with this.

If the file is big and you don't care about absolute portability, you can pretty easily seek to a point something like 4 kilobytes from the end, and start reading from there. When you figure out where the last line starts, use a platform-specific method to truncate the file to that point (SetEndOfFile on Windows, seek to the correct point and do a zero-length write on UNIX, etc.)

As far as the code you have goes, at least if you're trying to do this with a reasonably modern C++ compiler, you probably need to throw that code away and start over. I know that sounds harsh, and I'm really sorry, but in this case it's true. Let's start with the headers-- <iostream.h> and <fstream.h> have been obsolete since the mid-1990's or so, a couple of decades ago. Most modern compilers will reject the code outright on this basis alone.

From there, your code to read the data is fatally flawed in nearly all respects. The form of std::getline you're using is still technically included in C++, but it's been a good decade since it was used often enough to notice. You nearly always want the form that takes an std::string parameter. Your loop condition ( while(!inFile.eof()) ) is also completely broken--it pretty much can't work correctly.

As for how I'd do it, I'd probably do something on this general order:

read a line into `prev_line`
while (read a line into `current_line`) {
    write out prev_line
    prev_line = current_line
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM