忽略文本文件中的某些行(C#Streamreader)

时间:2014-08-12 14:13:22

标签: c# streamreader

我试图找出一种从我正在编写的程序中删除记录的方法。我有一个文本文件,其中所有客户数据分布在一组行中,我一次读取这些行并将它们存储在列表中

写作时我只是附加到文件中。但是,对于删除,我有想法在不再需要的行的前面添加*或#等字符。但是我不确定如何做到这一点

以下是我如何正确阅读数据:

提前致谢

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();

2 个答案:

答案 0 :(得分:0)

由于你使用字符串数组,你可以只使用cusdata [index] =“#”+ cusdata [index]将它附加到行的开头。但是,如果您的问题是如何从文件中删除它,为什么不跳过上述步骤而只是在编写文件时不添加要删除的行?

答案 1 :(得分:0)

这是一个适合您需求的小型读/写样本。如果没有,请在评论中告诉我。

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);
            }
        }
    }
}

我使用了以下&#34;数据样本&#34;为此

Matt Damon 100 222
Julia Roberts 125 152
Robert Downey Jr. 150 402
Tom Hanks 55 932