File output, backspacing

This is a discussion on File output, backspacing within the C++ Programming forums, part of the General Programming Boards category; I'm sorry if this has been asked before, but I couldn't find anything on the search I did. What i'm ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    29

    Question File output, backspacing

    I'm sorry if this has been asked before, but I couldn't find anything on the search I did.

    What i'm doing is simple enough, i'm just outputting some strings to a file, using "outfile<<string1" for example.

    My question is, how do I do the equivilent of a backspace? I want to remove the last character in the file, but i'm not sure how.
    I've tried using seekp/put to put in the '\b' backspace character, but that didn't work, cause I don't think thats really how i'm supposed to do it.

    I'm not posting any code cause I don't think its needed, seeing as this is a pretty basic thing, but if its really needed I will.

    Thanks for any suggestions.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could try something like this -

    Code:
    #include <fstream>
    using namespace std;
    
    
    int main()
    {
    
    	char a[]="Hello,World!";
    	ofstream os("bs.txt");
    	os << a;
    	os.seekp(-1,ios::cur);
    	os << '\0';
    
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    29

    Angry

    Thanks for trying, but that didn't seem to do it

    I can't believe this isn't a simple thing, I just can't think of what to do.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Why didn't it work? What result did you get? Are you trying to open a file that already exists? Do you need to seek to the end of the file before removing the character? Perhaps some code would help.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    29

    Smile

    Never mind, I figured it out.

    When I used your code, the problem was it was (for some reason) just putting in a blank space, and moving the last character over.
    like:

    (before) - ABC
    (after) - AB C

    But I fixed the problem by using .write, which lets me overwrite the last character.

    Thanks though, you pointed me in the right direction with your code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21