Click to See Complete Forum and Search --> : Append text at a certain row in a txt-file
korven
Nov 2nd, 2006, 07:38 AM
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.
wild_bill
Nov 2nd, 2006, 04:41 PM
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();
}
korven
Nov 3rd, 2006, 01:40 AM
Works great!
Thanks a lot!
basti42
Mar 15th, 2007, 07:52 PM
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
wild_bill
Mar 16th, 2007, 09:48 AM
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();
}
basti42
Mar 16th, 2007, 06:22 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.