tags:

views:

152

answers:

6

i have text file which i want to erase in C++ and python . How to do it?Do i have to assign the file pointer to a null value string?

A: 

Since text files are sequential, you can't directly erase data on them. Your options are:

  • The most common way is to create a new file. Read from the original file and write everything on the new file, except the part you want to erase. When all the file has been written, delete the old file and rename the new file so it has the original name.
  • You can also truncate and rewrite the entire file from the point you want to change onwards. Seek to point you want to change, and read the rest of file to memory. Seek back to the same point, truncate the file, and write back the contents without the part you want to erase.
  • Another simple option is to overwrite the data with another data of same length. For that, seek to the exact position and write the new data. The limitation is that it must have exact same length.

Look at the seek/truncate function/method to implement any of the ideas above. Both Python and C have those functions.

nosklo
i just want to erase the whole file from the begining
mekasperasky
Then just remove the file. Use `os.remove` or `os.unlink` in python, or `unlink` in C.Another option is to just reopen the file for writing or use `truncate`.
nosklo
A: 

You cannot "erase" from a file in-place unless you need to erase the end. Either be content with an overwrite of an "empty" value, or read the parts of the file you care about and write it to another file.

Ignacio Vazquez-Abrams
ok how to overwrite it without giving garbage value? i mean suppose a string length of 9 is there and i am overwriting it with string length of 6 . that would mean 3 garbage values rt?
mekasperasky
Correct. Plus you would have to have some way of identifying to any reader the fact that those 3 characters are garbage.
Ignacio Vazquez-Abrams
the fact is my code is spanned between python and c++ . so if a file read is done in C++ and file write in python .. both the time the file has to be erased..
mekasperasky
+7  A: 

In python:

open('file.txt', 'w').close()

Or alternatively, if you have already an opened file:

f = open('file.txt', 'r+')
f.truncate()

In C++, you could use something similar.

ondra
`#include<fstream>` and then `std::ofstream("file.txt");` about as short as in Python. :)
wilhelmtell
the reason this works (in both C++ and python) is because by default when you open a file for writing, it truncates the existing contents. So really it's sorta a side effect, and thus I would prefer the explicit call to truncate() for clarity reasons, even though it is unnecessary.
rmeador
+1  A: 
open("filename","w").close()
Douglas Leeder
It is perfect, it is exactly what mekasperasky is looking for !
Natim
A: 

Assigning the file pointer to null inside your program will just get rid of that reference to the file. The file's still there. I think the remove() function in the c stdio.h is what you're looking for there. Not sure about Python.

Hober
Alternatively use unlink() in unistd.h.
Void
BTW, both remove() and unlink() will decrease the reference count on a regular file, not actually erase it. The file will be erased once the reference count drops to zero. For example, if your process has the file open and you call unlink() or remove() on it, it won't be deleted until the file is close()d (i.e. ref count drops to zero - also assumes no other processes have the file open). This behavior is useful, for example, to ensure temporary files are not left around after abnormal process termination (e.g. open(), unlink(), perform file IO, **CRASH**, close() NOT called).
Void
+3  A: 

You have to overwrite the file. In C++:

#include <fstream>

std::ofstream("test.txt", std::ios::out).close();
Alex - Aotea Studios
This is good for C++ :)
Natim
There's no need for the `close()` call. The destructor will close the file. So you just need to create a temporary: `ofstream("test.txt");`
wilhelmtell