I need to develop a Phonebook.
I write my contacts data to a text file and I work with console. What options do I have to SEARCH and DELETE a contact in that text file?
This is how I am inserting contacts:
public class Writer
{
public void writer (string name,string lastname,string number)
{
StreamWriter Wrt = new StreamWriter("D:\\Sample.txt",true);
Wrt.Write(name);
Wrt.Write(lastname);
Wrt.Write(number);
Wrt.Write("#");
Wrt.Write("");
Wrt.Close();
}
}
Each line correspond to a contact, right? What is your criteria to delete a contact? If you are looking for a name and lastname, you can do this.
This will read your file and write all contacts you want to keep in another file.
However, if you want to keep the same file (or if your contacts info are on more than one line). You can read the whole file and keep it in memory, remove those you do not want, and then write the file again.
That was if you want to keep your text file. As it was suggested earlier, you can use xml to write and read an easier to maintain file. If you know the basics of xml, or up for a little challenge (and have time for it), it might be a better way to go.