All pastes #805689 Raw Edit

Stuff

public text v1 · immutable
·published 2007-12-04 15:56 UTC
rendered paste body
// CLASS FileIO

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PhoneBook
{
    class FileIO
    {
        public string name;
        public string phone;
        //public FileIO(string _name, string _phone)
        //{
        //    name = _name;
        //    phone = _phone;
        //}
        public void add()
        {
            StreamWriter fw = File.AppendText("phones.txt");
            fw.WriteLine(name + "   " + phone);
            fw.Close();
        }

        public string search()
        {
            string buffer = null;
            string result = null;
            int compareResult = 0;
            if(!(File.Exists("phones.txt")))
            {
                result = "Error no phone index.";
            }
            else 
            {
                StreamReader fr = File.OpenText("phones.txt");
                while ((buffer = fr.ReadLine()) != null)
                {
                    if ((compareResult = (buffer.IndexOf(name))) == 0)
                    {
                        result = buffer;
                        break;
                    }
                    else
                    {
                        result = "Not Found";
                    }
                }
                fr.Close();
            }
            return result;
        }

        public string delete()
        {
            string buffer = null;
            string result = "Not Found";
            int count = 0;
            int compareResult = 0;
            if (!(File.Exists("phones.txt")))
            {
                result = "Error no phone index.";
                count = 1;
            }
            else
            {
                StreamWriter fw = new StreamWriter("temp.txt");
                StreamReader fr = File.OpenText("phones.txt");
                while ((buffer = fr.ReadLine()) != null)
                {
                    if ((compareResult = (buffer.IndexOf(name))) == 0)
                    {
                        result = "Record deleted.";
                    }
                    else
                    {
                        fw.WriteLine(buffer);
                    }
                }
                fr.Close();
                fw.Close();
            }
            File.Delete("phones.txt");
            if (count == 0)
            {
                File.Move("temp.txt", "phones.txt");
            }
            return result;

        }
    }
}