0

I'm trying to work out a way of removing records from a program I'm writing. I have a text file with all the customer data spread over a set of lines and I read in these lines one at a time and store them in a List

When writing I simply append to the file. However, for deleting I had the idea of adding a character such as * or # to the front of lines no longer needed. However I am unsure how to do this

Below is how I currrently read the data in:

Thanks in advance

StreamReader dataIn = null;
        CustomerClass holdcus; //holdcus and holdacc are used as "holding pens" for the next customer/account
        Accounts holdacc;
        bool moreData = false;
        string[] cusdata = new string[13]; //holds customer data
        string[] accdata = new string[8]; //holds account data


        if (fileIntegCheck(inputDataFile, ref dataIn))
        {
            moreData = getCustomer(dataIn, cusdata);

            while (moreData == true)
            {
                holdcus = new CustomerClass(cusdata[0], cusdata[1], cusdata[2], cusdata[3], cusdata[4], cusdata[5], cusdata[6], cusdata[7], cusdata[8], cusdata[9], cusdata[10], cusdata[11], cusdata[12]);
                customers.Add(holdcus);
                int x = Convert.ToInt32(cusdata[12]);
                for (int i = 0; i < x; i++) //Takes the ID number for the last customer, as uses it to set the first value of the following accounts
                {                                                     //this is done as a key to which accounts map to which customers
                    moreData = getAccount(dataIn, accdata);
                    accdata[0] = cusdata[0];
                    holdacc = new Accounts(accdata[0], accdata[1], accdata[2], accdata[3], accdata[4], accdata[5], accdata[6], accdata[7]);
                    accounts.Add(holdacc);
                }


                    moreData = getCustomer(dataIn, cusdata);
            }
        }
        if (moreData != null) dataIn.Close();
  • I asssume this is a school project since you are using textfile to store customer data? Or is there any other reason you are not using database? – Tomi Niemenmaa Aug 12 '14 at 14:38
  • Its a first year university project. I'm studying Computer and Information Security so Programming is a bit of a side line – Shaun Flint Aug 12 '14 at 15:54
  • Also, I had the idea of writing all the customers I want to keep to a new text file, delete the old one and rename the new one to the old ones name? Do you think this will work? – Shaun Flint Aug 12 '14 at 15:57
  • It would work, but the approach MulletTron already suggested is the way to go. Read the file into memory (in your case the array of strings) and then rewrite the file without the rows you dont want. I'll try to fix you up a better answer today. – Tomi Niemenmaa Aug 13 '14 at 06:44

2 Answers2

0

Since your using string arrays, you can just do cusdata[index] = "#"+cusdata[index] to append it to the beginning of the line. However if your question is how to delete it from the file, why not skip the above step and just not add the line you want deleted when writing the file?

DDushaj
  • 127
  • 8
  • The problem is, the same text file is using for both reading and writing. I can add customers to the datafile fine, just not remove them – Shaun Flint Aug 12 '14 at 15:56
0

Here is a small read / write sample that should suit your needs. If it doesnt then let me know in the comment.

class Program
{
    static readonly string filePath = "c:\\test.txt";

    static void Main(string[] args)
    {
        // Read your file
        List<string> lines = ReadLines();

        //Create your remove logic here ..
        lines = lines.Where(x => x.Contains("Julia Roberts") != true).ToList();

        // Rewrite the file
        WriteLines(lines);


    }

    static List<string> ReadLines()
    {
        List<string> lines = new List<string>();

        using (StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open)))
        {
            while (!sr.EndOfStream)
            {
                string buffer = sr.ReadLine();
                lines.Add(buffer);

                // Just to show you the results
                Console.WriteLine(buffer); 
            }
        }

        return lines;
    }

    static void WriteLines(List<string> lines)
    {
        using (StreamWriter sw = new StreamWriter(new FileStream(filePath, FileMode.Create)))
        {
            foreach (var line in lines)
            {
                sw.WriteLine(line);
            }
        }
    }
}

I used the following "data sample" for this

Matt Damon 100 222
Julia Roberts 125 152
Robert Downey Jr. 150 402
Tom Hanks 55 932
Tomi Niemenmaa
  • 650
  • 4
  • 12
  • Hi, Thank you for providing this, a couple of questions though. Due to the project criteria, I have to use an ArrayList. This shouldn't make too much difference if I am correct? Also, my datafile has each customer spread over multiple lines, so I would have to just implement a loop to Read all the lines in? – Shaun Flint Aug 14 '14 at 19:39
  • Hi, Array list also implements the IList interface so other switching List to ArrayList there shouldn't be any difference in that area. Depending on how the different customer sections are defined you need to implement that logic to WriteLines method. Lets say you have a 3 lines per customer then you might want to find the first row and delete that row and the following two rows. Hope this helps! If it does I would hope you would mark the answer correct. If not just hit me with a question and I'll try to answer you. – Tomi Niemenmaa Aug 14 '14 at 20:16
  • Ok, I'll definitely give this a go! – Shaun Flint Aug 14 '14 at 21:48