how to delete the entry from file as i have following code for getusername() Code: override public void getUserName() { try { file = new FileStream(@"C:\username.txt", FileMode.Append, FileAccess.Write); // Create a new stream to write to the file sw = new StreamWriter(file); Console.WriteLine("Please Enter your username :"); s = Console.ReadLine(); Counter = Counter + 1; sw.WriteLine(Counter+"."+s); //sw.Write(sw.NewLine); Console.WriteLine("Please Enter your Phone Number :"); String ph = Console.ReadLine(); int i = Int32.Parse(ph); sw.WriteLine(i); sw.Write(sw.NewLine); // Close StreamWriter sw.Close(); // Close file file.Close(); } What can be used to delete the data from file? I am mainly interested if every entry should be removed like following File username.txt contains 1.haroon 123456 1.shah 78 Please enter username to delete the data: haroon Now it should delete haroon and 123456. How this delete functionality can be implemented? Any idea please help
The best possible way is to have something like read the line and have the line marked as deleted with some mark and have other program which cleans up the deleted stuff.
Ok I understood what you meant but "other program which cleans up the deleted stuff." can you small bit in english statement or pseudo code tell me how will be cleaning functionality?
Something like it runs through all the record, Empties the complete file and writes only those which does not have the deleted words in it.
Say you have the following operations 1. Insert 2. Delete 3. View Now when you insert a record in the file the following fields are entered ID :: Name :: Deleted 1 :: Haroon :: No 2 :: Shah :: No Now when any one would like to delete the record no 1 then it should be as follows ID :: Name :: Deleted 1 :: Haroon :: Yes 2 :: Shah :: No And when some body would like to view the records you should always display them those which does not have the deleted field as Yes Now keeping the deleted rows in the file is not desirable and so you can have another option to compact the file which just takes the content of the entire file into an array deletes the file and then writes back the file with those list that have Deleted not as yes. Remember :: is just a Delimiter of fields. Also remember that Having Deleted as No can be an optional and by default it could be No if nothing is there in the field.