Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Delete the entry from file (http://www.go4expert.com/forums/delete-entry-file-t3841/)

shah123 12Apr2007 18:19

Delete the entry from file
 
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

shabbir 12Apr2007 19:03

Re: Delete the entry from file
 
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.

shah123 12Apr2007 19:06

Re: Delete the entry from file
 
What will be delete statement look like?

shabbir 12Apr2007 19:09

Re: Delete the entry from file
 
Something like 1. haroon [Deleted]

shah123 12Apr2007 19:11

Re: Delete the entry from file
 
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?

shabbir 12Apr2007 19:15

Re: Delete the entry from file
 
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.

shah123 12Apr2007 19:27

Re: Delete the entry from file
 
I didnt get you Brother. Can you be more help me in small bit coding example?

shabbir 12Apr2007 20:35

Re: Delete the entry from file
 
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.


All times are GMT +5.5. The time now is 15:54.