Results 1 to 6 of 6

Thread: Append text at a certain row in a txt-file

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    Append text at a certain row in a txt-file

    Hi.

    I'm trying to append text to a txt file at a certain row.
    For example:
    a
    b
    c
    d

    And after I run the program and append 'z' at row 3, it should look like:
    a
    b
    cz
    d

    Does anyone know how to do this? I can't seem to get append to work, only overwriting.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Append text at a certain row in a txt-file

    Code:
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			System.Text.StringBuilder buffer = new System.Text.StringBuilder();
    			System.IO.StreamReader reader = System.IO.File.OpenText("C:\\Temp\\New Text Document.txt");
    			Int32 lineCount = 1;
    
    			while (reader.Peek() != -1)
    			{
    				if (lineCount == 3)
    				{
    					buffer.Append(reader.ReadLine());
    					buffer.Append("z");
    				}
    				else
    				{
    					buffer.Append(reader.ReadLine());
    				}
    				buffer.Append(Environment.NewLine);
    				lineCount++;
    			}
    			reader.Close();
    
    			System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\Temp\\New Text Document.txt",false);
    			writer.Write(buffer.ToString());
    			writer.Flush();
    			writer.Close();
            }

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    Re: Append text at a certain row in a txt-file

    Works great!
    Thanks a lot!

  4. #4
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: Append text at a certain row in a txt-file

    these codes works
    private void button1_Click(object sender, System.EventArgs e)
    {
    System.Text.StringBuilder buffer = new System.Text.StringBuilder();
    System.IO.StreamReader reader = System.IO.File.OpenText("C:\\Temp\\New Text Document.txt");
    Int32 lineCount = 1;

    while (reader.Peek() != -1)
    {
    if (lineCount == 3)
    {
    buffer.Append(reader.ReadLine());
    buffer.Append("z");
    }
    else
    {
    buffer.Append(reader.ReadLine());
    }
    buffer.Append(Environment.NewLine);
    lineCount++;
    }
    reader.Close();

    System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\Temp\\New Text Document.txt",false);
    writer.Write(buffer.ToString());
    writer.Flush();
    writer.Close();
    }

    but how if i would like these output?
    a b c d
    1 1 1 1
    2 2 2 2
    3 3 3 3
    4 4 4 4

    tnx

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Append text at a certain row in a txt-file

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    
    	string myPath = "C:\\New Text Document.txt";
    	if (! System.IO.File.Exists(myPath))
    	{
    		WriteData(string.Empty,myPath);
    	}
    
    	System.Text.StringBuilder buffer = new System.Text.StringBuilder();
    	System.IO.StreamReader reader = System.IO.File.OpenText(myPath);
    	bool firstLine = true;
    
        while (reader.Peek() != -1)
    	{
    		string currentLine = reader.ReadLine();
    					char lastChar = Convert.ToChar(currentLine.Substring(currentLine.Length - 1));
    		
    		if (firstLine)
    		{
    			lastChar = (char)(lastChar + 1);
    			firstLine = false;
    		}
    
    		buffer.Append(currentLine);
    		buffer.Append(" ");
    		buffer.Append(lastChar);
    		buffer.Append(Environment.NewLine);
    	}
    	reader.Close();
    
    	//handle the first write
    	if (firstLine)
    	{
    		buffer.Append("a");
    		buffer.Append(Environment.NewLine);
    		for (int i = 1; i <= 4; i++)
    		{
    			buffer.Append(i.ToString());
    			buffer.Append(Environment.NewLine);
    		}
    	}
    	WriteData(buffer.ToString().TrimEnd(),myPath);
    }
    
    private void WriteData(string data,string path)
    {
    	System.IO.StreamWriter writer = new System.IO.StreamWriter(path,false);
    	writer.Write(data);
    	writer.Flush();
    	writer.Close();
    }
    Last edited by wild_bill; Mar 16th, 2007 at 12:14 PM.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: Append text at a certain row in a txt-file

    FileStream stmRecords = new FileStream(@"C:\Scanned.txt", FileMode.Append, FileAccess.Write, FileShare.None);
    StreamWriter stmWriter = new StreamWriter(stmRecords);

    stmWriter.WriteLine(var1 + " , " + var2);

    stmWriter.Flush();
    stmWriter.Close();


    tnx for ur reply but this is what i searched. it's kinda easy to understand.
    except the stmWriter.Flush(); i don't jknow its function.
    but if someone has the same problem above code will do.

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