DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2006
    Posts
    91

    Appending a New line at specified location in a Text file

    Hi
    I have a “Make File” (.mak). Please see below partial “make file” below:-
    # The nmake reference can be found here:
    # http://msdn2.microsoft.com/en-us/lib...ha(VS.80).aspx

    .SILENT:

    LRBSDKDIR=$(LRB_ABS_PATH)
    !INCLUDE <$(LRB_ABS_PATH)\lrb.mak>
    *****End of Make File*********

    I am trying to append a new line into to this “make file” at specific location, just after the line “.SILENT:”
    And before line starting with “LRBSDKDIR=$(LRB_ABS_PATH)”. But not able to append at that place.

    Please see the code below:-

    string fileName = @"c:\srini1981\rt_aa\lrb.mak";
    using (StreamWriter sw2 = new StreamWriter(fileName,true))
    {
    sw2.Write("LRB_ABS_PATH=C:\\perforce\\AVC\\mainline\\AVCSDK");

    }

    The above code appends the New line at the End of the make file, which is not I want. So how can I make it to append in-between those two lines specified above. Also there is lot of space between those two line, were in
    I want to append this new line

    Thanks in advance,

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    Could you read the file into a textbox, remove the blank lines, add your line, and write it back out again? Would that work?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Sep 2007
    Posts
    86

    RE: Appending a New line at specified location in a Text file

    Here is a sample console application that will accomplish this for you. You could get fancier and have it simply replace the existing file by opening the file and editing it in place if you want too.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamWriter sw = new StreamWriter("NewFile.txt");
    
                using(StreamReader sr = new StreamReader("textfile1.txt",Encoding.ASCII))
                {
                    string line;
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                        sw.WriteLine(line);
                        if (line.StartsWith("B"))
                        {
                            sw.WriteLine("\n\r");
                        }                    
                    }
                }
                sw.Close();
            }
        }
    }
    Here is what TextFile1.txt looks like:

    Code:
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    John Wiese
    ISV Architect Evangelist - Microsoft
    http://blogs.msdn.com/usisvde

Similar Threads

  1. Replies: 14
    Last Post: 11-29-2005, 06:56 AM
  2. File Locking a Text File on a Network Drive
    By Miles Tones in forum VB Classic
    Replies: 1
    Last Post: 05-20-2005, 01:57 PM
  3. Delete a line from a text file
    By S. Palliayaguruge in forum Java
    Replies: 1
    Last Post: 07-23-2002, 11:14 AM
  4. appending text
    By vinman in forum VB Classic
    Replies: 1
    Last Post: 04-11-2001, 07:48 AM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links