Reputation: 5267
I opened a (text) file, like so: File *fp = fopen("findPattern2.txt", "w+");
in which i have written text. The last character i have in there is a ,
. Now i want to delete that character using backspace. I've read the answers here that BACKSPACE only moves the cursor, then i have to overwrite that character by writing on top of it and this is what i do. So I did fprintf(fp, "\b \b")
to completely erase the ,
, but instead this is what the file shows afterwards:,BS BS
. All of these work with a terminal. Why not with a file?
From what I gather the only way to do this to a file would be if i memory mapped it, edit its contents, copy them to a new file and delete the old one.
Upvotes: 2
Views: 943
Reputation: 8142
A file is just a collection of bytes that when read in by a program produce (possibly) meaningful results like a picture or video or whatever. Unless the program knows that a backspace character should delete the proceeding character, it won't do anything.
Your terminal is a program that has been written in just that way to react to special characters like backspace and knows what to do when it receives one though.
Upvotes: 6
Reputation: 399843
Cursors and line editing are things that consoles and terminals have, not files.
You seem to misunderstand how files work.
There's simply no concept of editing inside a file, it's a sequence of bytes. You can overwrite by using fseek()
to move the current location (sometimes called "the cursor" but that's an analogy, it doesn't mean files act like text editors) and then writing the new data.
Upvotes: 3
Reputation: 50774
If you "printf" the BS
character to the terminal, the latter interprets this so the cursor will be moved to the left.
But if you "fprintf" the BS
character to a file, then the BS
character (which is just a byte) will simply be written into the file.
Upvotes: 4