...

View Full Version : Write "backspace" to a file



rayhuang0924
06-07-2007, 09:26 AM
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.

smalldog
06-07-2007, 01:07 PM
Hello, why you want to write backspace character to a file?

rayhuang0924
06-07-2007, 01:12 PM
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

smalldog
06-07-2007, 01:30 PM
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..

aedrin
06-07-2007, 02:53 PM
On a side note, this:


fwrite($testfile,'\b');

Should be:


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.

kbluhm
06-07-2007, 03:09 PM
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.

rayhuang0924
06-08-2007, 05:10 AM
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

aedrin
06-08-2007, 03:00 PM
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:



fwrite($fp, '\n');


You'll see \n show up in your file.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum