1

I have a text file into which I write first 10 characters the user inputs:

int x=0;
ofstream fout("out.txt"); 
while (x<=10)
{
   char c=getch();
   if (c==8)//check for backspace
      fout<<'\b';
   else
      fout<<c;
   x++;
}

Whenever the user presses backspace I want to delete the previously entered character from the text file.

I tried writing '\b' to the file but it doesn't delete the last character.

How can I do so?

Thanks

m0bi5
  • 7,560
  • 7
  • 28
  • 41
  • One option would be to use seekg or seekp (Random file I/O operators) to move the pointer and then write a blank space. However, this is really crude and I believe something better should exist. – therainmaker Dec 07 '15 at 14:08
  • @therainmaker i tried that too but it doesnt work – m0bi5 Dec 07 '15 at 14:10
  • 6
    A cleaner way to do this would be to keep the data until your are "done", and *then* write it to a file. The other way would be to read the entire file, then write everything but the last character. – crashmstr Dec 07 '15 at 14:11
  • @crashmstr i have used 10 as an example, the actual program runs infinite number of times until the user stops it – m0bi5 Dec 07 '15 at 14:12
  • @tobi303 each character is 1 byte so I should shift the filepointer by 1 each time the user presses back space am I right? – m0bi5 Dec 07 '15 at 14:15
  • @user4748321 : Are you sure you are doing it correctly? Because I have overwritten files in the past by moving the pointer. To confirm, you are writing `" "` after shifting, right? – therainmaker Dec 07 '15 at 14:17
  • http://php.net/manual/en/function.ftruncate.php – Karoly Horvath Dec 07 '15 at 14:29
  • @KarolyHorvath is it there for c++ too? – m0bi5 Dec 07 '15 at 14:30
  • oops wrong link. obviously. google? – Karoly Horvath Dec 07 '15 at 14:35
  • Possible duplicate of [C++ delete last character in a txt file](http://stackoverflow.com/questions/9529027/c-delete-last-character-in-a-txt-file) – CppNITR Dec 07 '15 at 14:52

2 Answers2

1

If I correctly understand, your requirement is that a BS should move the file pointer one position back. That's just what seekp is done for.

In windows (because of getch...) the following just meet the requirement:

int x=0;
ofstream fout("out.txt"); 
while (x<=10)
{
    char c=getch();
    if (c==8) { //check for backspace
        fout.seekp(-1, std::ios_base::end);
        if (x > 0) x -= 1; // ensure to get 10 characters at the end
    }
    else {
        fout<<c;
        x++;
    }
}   return 0;

It works here because the last character is overwritten. Unfortunately, as this other question confirms, there is no standard way to truncate an open file with fstream.

Community
  • 1
  • 1
Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199
0

There's no simple way by which you can delete the last character of the file in C++. You have to go the trivial way ie :-

Read the contents of the file except the last one & copy it to another file

You can either use input-output iterators or strings or both for the same.

Ankit Acharya
  • 2,225
  • 1
  • 13
  • 26