Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Write "backspace" to a file

    Dear all:

    I want to ask a stupid question: How to write a backspace character to a file? For example, I wrote following codes:

    $testfile=fopen("test.txt","r+");
    fwrite($testfile,'\b');
    fclose($testfile);

    However, it only output "\b" rather than a "real" backspace. It works if I substitute '\n' for '\b', which will output a new line. Can anyone tell me how to do this? Thanks a lot.

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello, why you want to write backspace character to a file?
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #3
    New to the CF scene
    Join Date
    May 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by smalldog View Post
    Hello, why you want to write backspace character to a file?
    I want to delete a line in a middle of a text file and I don't want to rewrite lines after the line to the start position of this line. Do you have other ways to achieve the goal? Thanks a lot!

    Ray

  • #4
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You must to rewrite the file manually, you can't achieve that goal with writing backspace character and think that computer will press the backspace in that place , computer will just store some character no matter which, and that's the end..
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    On a side note, this:

    PHP Code:
    fwrite($testfile,'\b'); 
    Should be:

    PHP Code:
    fwrite($testfile,"\b"); 
    Within single quotes, PHP does no parsing. Only within double quotes would a special character like that (were it to exist/work) be translated.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by rayhuang0924 View Post
    I want to delete a line in a middle of a text file and I don't want to rewrite lines after the line to the start position of this line. Do you have other ways to achieve the goal? Thanks a lot!
    You've just hit upon one of the many major advantages SQL has over text file databases.

    If you want to remove one line, the two easiest ways to achieve this:
    1. For smaller files, suck the file into memory as an array with file(), remove the unwanted line[s], and rewrite the entire file.
    2. For larger files, open a new file and iterate the old file line by line, writing each line to the new file while skipping over the unwanted line[s]. Then unlink() the old file and rename() the new file using the old name.

    That's probably as good as it gets when you're dealing with text databases that store each record on a new line. It's a bit better when you store serialized arrays, but not by much. And when dealing with a high-traffic site it's not the most efficient way to go about things.

  • #7
    New to the CF scene
    Join Date
    May 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi all:

    Thanks for your reply, I've moved the text entries to MySQL database and it works well.

    To Aedrin: I 've tried "\b" but it still failed. Thanks for your suggestion.

    Ray

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    To Aedrin: I 've tried "\b" but it still failed. Thanks for your suggestion.
    I was not suggesting that it would work. Merely that if \b was in fact something that would work, it would have to be within double quotes. Try adding a new line like this:

    PHP Code:
    fwrite($fp'\n'); 
    You'll see \n show up in your file.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •