G
Guest
I have a file opened as a FileStream.
I have a StreamReader and StreamWriter object opened, referencing the
FileStream object.
My aim is to read lines from the file until I find the line where there is a
unique comment as a place holder.
I would then like to use the StreamWriter to insert lines after this comment
and save the file.
How do I instruct the StreamWriter to find the line position based on the
current position that the StreamReader is in?
Code so far:
FileStream fs = new FileStream("FILENAME",FileMode.Open,
FileAccess.ReadWrite);
using(StreamReader sr = new StreamReader(fs))
{
sr.BaseStream.Seek(0,SeekOrigin.Begin);
string line = sr.ReadLine();
while(!line.StartsWith("// start"))
{
line = sr.ReadLine();
}
// at this point the reader should be at the correct position within the file
using(StreamWriter sw = new StreamWriter(fs))
{
sw.BaseStream.Seek(sr.BaseStream.Position, SeekOrigin.Current);
sw.WriteLine("test");
sw.Flush();
}
}
fs.Close();
I have a StreamReader and StreamWriter object opened, referencing the
FileStream object.
My aim is to read lines from the file until I find the line where there is a
unique comment as a place holder.
I would then like to use the StreamWriter to insert lines after this comment
and save the file.
How do I instruct the StreamWriter to find the line position based on the
current position that the StreamReader is in?
Code so far:
FileStream fs = new FileStream("FILENAME",FileMode.Open,
FileAccess.ReadWrite);
using(StreamReader sr = new StreamReader(fs))
{
sr.BaseStream.Seek(0,SeekOrigin.Begin);
string line = sr.ReadLine();
while(!line.StartsWith("// start"))
{
line = sr.ReadLine();
}
// at this point the reader should be at the correct position within the file
using(StreamWriter sw = new StreamWriter(fs))
{
sw.BaseStream.Seek(sr.BaseStream.Position, SeekOrigin.Current);
sw.WriteLine("test");
sw.Flush();
}
}
fs.Close();