1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Delete the entry from file

Discussion in 'C#' started by shah123, Apr 12, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    What will be delete statement look like?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Something like 1. haroon [Deleted]
     
  5. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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.
     
  7. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    I didnt get you Brother. Can you be more help me in small bit coding example?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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.
     

Share This Page