0

I am writing a program which will store competitor information in a txt file. And when required, this program needs to wipe all the records from system with the click of a button. Now I am already able to save and load competitors in "test.txt" but I dont know how to delete already registered participators. Here is my partial code where I construct and register people.

public Competitor(string inCompetitorType, string inCompetitorName, string inCompetitorAddress, int inCompetitorScore, string inCompetitorNo)
    {
        CompetitorType = inCompetitorType;
        CompetitorNo = inCompetitorNo;
        CompetitorName = inCompetitorName;
        CompetitorAddress = inCompetitorAddress;
        CompetitorScore = inCompetitorScore;        
    }
public virtual void Save(string fileName)
    {
        StreamWriter outputFile = new StreamWriter(fileName, true);

        outputFile.WriteLine(CompetitorType);
        outputFile.WriteLine(CompetitorNo);
        outputFile.WriteLine(CompetitorName);
        outputFile.WriteLine(CompetitorAddress);
        outputFile.WriteLine(CompetitorScore);

        outputFile.Close();

    }

This bit is in my Storage class rather than my competitor class

 public string AddAmateurCompetitor(string inCompetitorName, string inCompetitor)
    {
        string CompetitorNo = GetNumber().ToString();
        Amateur newCompetitor = new Amateur(inCompetitorName, inCompetitorAdress, 0, CompetitorNo);
        Competitors.Add(CompetitorNo, newCompetitor);
        return CompetitorNo;
    }

Please suggest me ways the delete records function could be implemented

Umut
  • 7
  • 5
  • 1
    Why don't you just close the file and delete it with a regular system call? – Leonardo Alves Machado Apr 25 '16 at 18:04
  • 1
    Why don't you just delete the file? `File.Delete(fileName)` – ichramm Apr 25 '16 at 18:04
  • I need to delete the individual records instead of deleting the file because I need the file to be there the whole time. I want to implement the delete data function for the test records I will create in the original records txt file. – Umut Apr 25 '16 at 18:06
  • Or open it and overwrite all... [overwrite text file data?](http://www.cplusplus.com/forum/general/39093/) `file.open("file.ext",ios::out);` – DIEGO CARRASCAL Apr 25 '16 at 18:07
  • @Umut, do you need to delete lines or the full content? – DIEGO CARRASCAL Apr 25 '16 at 18:08
  • I need to delete full content of the txt file but I need to do this record by record with a loop to delete the remaining records each time one by one – Umut Apr 25 '16 at 18:10

2 Answers2

0

You may use this :

if(File.Exists(@"C:\test.txt"))
{
    File.Delete(@"C:\test.txt");
}

OR

File.WriteAllText(path, String.Empty);

OR

File.Create(path).Close();
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
0

If you want to delete the row not the file like you mention in your comment, you would first need to read the file. After the file is read you would iterate through the stream only adding the records you want to keep. You would then write that collection to your file.

    bool keep;
    List<string> list = new List<string>();
    using (System.IO.StreamReader inputFile = new System.IO.StreamReader(fileName))
    {
         string line;
         while ((line = inputFile.ReadLine()) != null)
         {
              //detemine if keep is true 
              //also do anything else you need to with the data from the file here
              if (keep) list.Add(line); 
         }
    }
    using (StreamWriter outputFile = new StreamWriter(fileName, false))
    {
         foreach(string line in list)
         {
              outputFile.WriteLine(line)
         }
    }

If the condition is always false every record would be deleted.

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57