5

I need some help on deleting the last character in a txt file. For example, if my txt file contains 1234567, I need the C++ code to delete the last character so that the file becomes 123456. Thanks guys.

Ð..
  • 270
  • 3
  • 16
  • 3
    what have you tried? show your code.We can help you to solve a specific problem or understand things which are strange to you but we won't write code instead of you! – shift66 Mar 02 '12 at 06:32
  • 1
    If the file is small enough to that you can read it into a buffer then why not do all the data manipulation you need to before you save it? – Edward Severinsen Sep 17 '18 at 03:37
  • Possible duplicate of [Removing Characters from a File](https://stackoverflow.com/questions/5503863/removing-characters-from-a-file) – mt3d May 08 '19 at 17:57

4 Answers4

5

The only way to do this in portable code is to read in the data, and write out all but the last character.

If you don't mind non-portable code, most systems provide ways to truncate a file. The traditional Unix method is to seek to the place you want the file to end, and then do a write of 0 bytes to the file at that point. On Windows, you can use SetEndOfFile. Other systems will use different names and/or methods, but nearly all will have the capability in some form.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
5

For a portable solution, something along these lines should do the job:

#include <fstream>

int main(){
    std::ifstream fileIn( "file.txt" );              // Open for reading
    std::string contents;
    fileIn >> contents;                              // Store contents in a std::string
    fileIn.close();
    contents.pop_back();                             // Remove last character
    std::ofstream fileOut( "file.txt", std::ios::trunc ); // Open for writing (while also clearing file)
    fileOut << contents;                             // Output contents with removed character
    fileOut.close();

    return 0;
}
Ð..
  • 270
  • 3
  • 16
Alex Z
  • 2,310
  • 2
  • 16
  • 23
3

Here's a more robust method, going off Alex Z's answer:

#include <fstream>
#include <string>
#include <sstream>

int main(){
    std::ifstream fileIn( "file.txt" );                   // Open for reading

    std::stringstream buffer;                             // Store contents in a std::string
    buffer << fileIn.rdbuf();
    std::string contents = buffer.str();

    fileIn.close();
    contents.pop_back();                                  // Remove last character


    std::ofstream fileOut( "file.txt" , std::ios::trunc); // Open for writing (while also clearing file)
    fileOut << contents;                                  // Output contents with removed character
    fileOut.close(); 
}

The trick is these lines, which allow you to read the entire file efficiently into the string, and not just a token:

    std::stringstream buffer;
    buffer << fileIn.rdbuf();
    std::string contents = buffer.str(); 

This is inspired from Jerry Coffin's first solution in this post. It is supposed to be the fastest solution there.

Ð..
  • 270
  • 3
  • 16
1

If the input file is not too large, you can do the following:-

1. Read the contents into a character array.
2. Truncate the original file.
3. Write the character array back to the file, except the last character.

If the file is too large, you can possibly use a temporary file instead of a character array. It will be a bit slow though.

Ð..
  • 270
  • 3
  • 16
vaisakh
  • 1,001
  • 9
  • 18