Hi Everyone,
Got one question here, let say I used fstream to write my data (username, password) into a password.txt files, how can I remove the particular user from the txt file when need to?
Hi Everyone,
Got one question here, let say I used fstream to write my data (username, password) into a password.txt files, how can I remove the particular user from the txt file when need to?
You need to read the entire file into memory, remove whatever data you want, and then write the file's data back to the disk.
I recommend writing to a temporary file, and then remove()ing the old file and rename()ing the new file to the old name. That way, if your program is interrupted while writing the data out to the file, the old file will still be in existence rather than being deleted. [edit] Those are C functions: include <cstdio>. [/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Not necessarily. If the format is for one entry per every line, then you only ever have to hold a single line in memory. Read a line -- if the line is for the user you want to delete, skip it. Otherwise, write it to the output file. Repeat. At the end, remove the original and rename the output back.