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

忽略文本文件中的某些行(C#Streamreader),c#,streamreader,C#,Streamreader,我正试图找到一种从我正在编写的程序中删除记录的方法。我有一个文本文件,所有客户数据分布在一组行中,我一次读取一行,并将它们存储在列表中 在编写时,我只是将其附加到文件中。然而,对于删除,我想到了在不再需要的行的前面添加一个字符,如*或#。然而,我不确定如何做到这一点 以下是我当前如何读取中的数据: 提前谢谢 StreamReader dataIn = null; CustomerClass holdcus; //holdcus and holdacc are used as "h

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

在编写时,我只是将其附加到文件中。然而,对于删除,我想到了在不再需要的行的前面添加一个字符,如*或#。然而,我不确定如何做到这一点

以下是我当前如何读取中的数据:

提前谢谢

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();
StreamReader dataIn=null;
客户类holdcus//holdcus和holdacc用作下一个客户/账户的“备用笔”
账户持有ACC;
bool-moreData=false;
字符串[]cusdata=新字符串[13]//保存客户数据
字符串[]accdata=新字符串[8]//保存帐户数据
if(fileintegracheck(inputDataFile,ref dataIn))
{
moreData=getCustomer(数据输入,cusdata);
while(moreData==true)
{
holdcus=新客户类别(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]);
添加(holdcus);
int x=转换为32(cusdata[12]);
for(int i=0;i
由于您使用的是字符串数组,因此只需执行cusdata[index]=“#”+cusdata[index]即可将其附加到行首。但是,如果您的问题是如何将其从文件中删除,为什么不跳过上述步骤,在写入文件时不添加要删除的行呢?

这里有一个小的读/写示例,应该适合您的需要。如果没有,请在评论中告诉我

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

我想这是一个学校项目,因为你使用文本文件存储客户数据?或者你们不使用数据库还有其他原因吗?这是大学一年级的项目。我在学习计算机和信息安全,所以编程有点像副业。所以,我有一个想法,把我想保留的所有客户都写在一个新的文本文件中,删除旧文件,然后将新文件重命名为旧文件名?你认为这行得通吗?行得通,但穆莱特伦已经提出的方法是可行的。将文件读入内存(在您的例子中是字符串数组),然后重写文件,不包含您不需要的行。今天我会给你一个更好的答案。谢谢!这将是非常有用的问题是,相同的文本文件用于读和写。我可以将客户添加到数据文件中,很好,只是不删除他们。嗨,谢谢你提供这个,不过还有几个问题。由于项目标准,我必须使用ArrayList。如果我是对的,这应该不会有太大的区别吧?此外,我的数据文件将每个客户分散在多行上,因此我必须实现一个循环来读取所有行?嗨,Array list还实现了IList接口,因此其他将列表切换到ArrayList的操作在这方面应该没有任何区别。根据不同客户部分的定义方式,您需要将该逻辑实现为WriteLines方法。假设每个客户有3行,那么您可能希望找到第一行并删除该行和以下两行。希望这有帮助!如果是的话,我希望你能把答案记对。如果你不只是问我一个问题,我会尽力回答你。好的,我一定会试试!
Matt Damon 100 222
Julia Roberts 125 152
Robert Downey Jr. 150 402
Tom Hanks 55 932