CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2006
    Posts
    5

    delete a line in a file

    Hello, do you know how can i delete a specific line in a text? Is there a specific command? Is it possible not lo leave the line blank and bring the next records one place up?
    Thanksssss

  2. #2
    Join Date
    Nov 2005
    Posts
    63

    Re: delete a line in a file

    I don't know if that's an already available command...but a function that will do it is:

    Code:
    public static void DeleteLine(String file, int line)
            {
                StreamReader streamIn = new StreamReader(file);
                String newFile = "";
                for (int i = 0; i < line && !streamIn.EndOfStream; i++)
                {
                    newFile += streamIn.ReadLine();
                    newFile += "\r\n";
                }
                streamIn.ReadLine();
                newFile += streamIn.ReadToEnd();
                streamIn.Close();
                StreamWriter streamOut = new StreamWriter(file);
                streamOut(newFile);
                streamOut.Flush();
            }

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: delete a line in a file

    Even better you should read the file into an ArrayList of strings per line, remove the line you want and write it out again.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured