开发者

How to Delete a Specific Line in a Text File?

I have a little problem. Let me explain the workplace.

My Text File Structure: X_OFFSET 0 Y_OFFSET 0 IDSTART 0001 222 222 222 222 0002 250 500 250 0003 1700 500 500 250 0004 900 600 500 250 0005 1350 250 500 250 IDEND

What I must do is to find the IDSTART-IDEND block and DELETE a spesific line between them.

Example: User set id1 123 124 125 126 => id1 means ID, 123 means Center X, 124 means Center Y, 125 means Center Z and 126 means Radius.

Workplace: The user will enter the id number(such as id5) to a text box, then click a button to delete the whole line starting with "id5"...

is this possible? if it is, How can I make it?

[Improved_v2]

Okey, I've made some changes and it works now. But, there is problem.

FileStream fs = new FileStream("C:\hucre.txt", FileMode.Open); StreamReader okuma = new StreamReader(fs); string Metin = okuma.ReadToEnd(); char[] Ayrac = { '\n' }; MetinSplit = Metin.Split(Ayrac);

    for (int i = 0; i < MetinSplit.L开发者_运维技巧ength; i++)
    {
        GeciciDizi.Add("a");
        GeciciDizi2.Add("b");
    }
    // MetinSplit'i GeciciDiziye At.
    for (int i = 0; i < MetinSplit.Length; i++)
    {
        GeciciDizi[i] = MetinSplit[i];
    }

    for (int i = 0; i < MetinSplit.Length; i++)
    {
        GeciciDizi2[i] = MetinSplit[i];
    }

    for (int i = 0; i < GeciciDizi.Count; i++)
    {
        if((string)GeciciDizi[i]==SilinecekDeger)
            GeciciDizi.RemoveAt(i);
    }
    for (int i = 0; i < GeciciDizi.Count; i++)
    {
        GeciciDizi2[i] = GeciciDizi[i];
    }

    okuma.Close(); 
    fs.Close();
    FileStream newFile = new FileStream("C:\\hucretemp.txt",      FileMode.Open);

    StreamWriter yaz = new StreamWriter(newFile);
    for (int i = 0; i < GeciciDizi2.Count; i++)
    {
        yaz.WriteLine(GeciciDizi2[i]);
    }

    yaz.Close();
    yeniFile.Close();
    File.Delete("C:\\hucre.txt");

    File.Move(@"C:\\hucretemp.txt",   @"C:\\hucre.txt");
}

}

Here, the problem is this.

When I read from the file hucre.txt to my tempArrayList, it wrtes to array like this below.

X_OFFSET 0\r\n Y_OFFSET 0\r\n IDSTART\r\n 0001 222 222 222 222\r\n 0002 250 500 250\r\n 0003 1700 500 500 250\r\n 0004 900 600 500 250\r\n 0005 1350 250 500 250\r\n IDEND

it adds one more \r.

So, when I want to read something from the "hucre.txt", I can't managed to do it, because it becomes like this 0001 222 222 222 222*\r\r\n*

How can I remove it?

I really am desperate, please help :/

My best regards...


I didn't read through all your code, but your I/O logic is way to complicated and inefficient.

Basic idea:

using (var reader = File.OpenText(orgFileName))
using (var writer = File.CreateText(tmpFileName))
{
    while (true)
    {
       string line = reader.ReadLine();
       if (line == null)
         break; // done

       // logic to analyze / track content

       if ( Is_special_line)
       {
          // do something, or maybe nothing (skip)
       }
       else
       {
          writer.WriteLine(line);
       }
    }
}

File.Delete(orgFilename);
File.Rename(tmpFilename, orgFilename);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜