Delete last character in a file

Posted on 2003-03-03
Medium Priority
Last Modified: 2012-06-21
Hello, I am writing a simple text editor and would like to figure out how to delete the last character in a file that I am writing to.  Right now when I hit backspace, in the dos window it moves back a character but in the text file I just get a big .  Here is my source so far so you can understand how this file is being written to:

void input_text()
     // Initialize variables and open file for writing
     char ch;
     ofstream textfile("textfile.txt");

     // Read in characters and store to file
     while (ch != ESC)
          ch = getch();
          if (ch != ESC)
               if (ch == ENTER)
                    ch = '\n';
               textfile << ch;
               cout << ch << flush;
     // Close file

I can read in backspace just like I can enter and esc but I don't know how to move one space back in the file.  Any help would be appreciated!
Question by:regan_a
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

Kocil earned 150 total points
ID: 8062342
It is strange to make an editor that directly write into the file. But here is the solution you ask

  while((ch = getch()) != ESC) {
      switch (ch) {
        case BACKSPACE:
           cout << BACKSPACE << ' ' << BACKSPACE;
        case ENTER : ch = '\n';
              textfile << ch;
              cout << ch << flush;

Thing will be easier if you buffer the input before write it to the textfile. For example
   char buffer[100;
   while(...) {
     textfile << buffer;


Expert Comment

ID: 8062529
>>>I am writing a simple text editor
then its better that u should use the buffer method.. load the file in -inmemory buffer do the changes and then save the buffer back to the file

editing straight to the file is nuthing but strange..

Author Comment

ID: 8062557
Oh, I know it is strange, I just wanted to see if it could be done :)  Regularly I would load it into the buffer but I am learning the language and trying to imagine as many abstract ways of learning different things as possible.  Anyways, thanks for the help/advice you two.

Expert Comment

ID: 8063648
small note--
almost anything can be done in programming world.. and somethings are preferred over most others

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question