Hello,
I'm having several methods which each apply an operation to the a textfile, where the next operation requires the result of the previous operation as input:
private TextReader input = new StreamReader("input.txt");
private TextWriter output = new StreamWriter("output.txt");
MemoryStream result_1 = new MemoryStream();
MemoryStream result_2 = new MemoryStream();
Operation_1(input, ref result_1);
Operation_2(result_1, ref result_2);
Operation_3(result_2, output);
The code for Operation_1:
private void Operation_1(TextReader input, ref MemoryStream output)
{
TextWriter outputWriter = new StreamWriter(output);
String line;
while (input.Peek() >= 0) //while not end of file
{
line = input.ReadLine();
//perform operation on line
outputWriter.writeline(line);
}
input.Close();
}
the code for operation_2:
private void Operation_2(TextReader input, ref MemoryStream output)
{
input.Seek(0, SeekOrigin.Begin); //reset stream to start of file
TextReader inputReader = new StreamReader(input);
TextWriter outputWriter = new StreamWriter(output);
String line;
while (inputReader.Peek() >= 0) //while not end of file
{
line = inputReader.ReadLine();
//perform operation on line
outputWriter.writeline(line);
}
inputReader.Close();
}
The code for operation_3:
private void operation_3(MemoryStream input, TextWriter output)
{
input.Seek(0, SeekOrigin.Begin); //reset stream to start of file
TextReader inputReader = new StreamReader(input);
String line;
while (inputReader.Peek() >= 0) //while not end of file
{
line = inputReader.ReadLine();
//perform operation on line
output.writeline(line);
}
inputReader.Close();
output.Close();
}
Now the problem is that i'm not getting the same result as storing each intermediate result to a physical txt file on the harddisk and using that file for the next operation. A few lines and the end of the file is missing.
Also this seems like not a very clean and generic way of doing it.
So hence my question; why are my results different when using MemoryStream for the intermediate results and is there a cleaner, more flexible way of doing this? (I want to work towards a solution were it is possible to choose if you want to save the intermediate results or not).
Loading
VulpesPosted May 6, 2011, 10:31 AM
wouterkPosted May 6, 2011, 9:10 AM
"I don't really think there is a more elegant way of doing it apart from saving the intermediate results to disk after each operation for which, of course, you already have code. "
Okay I guess i'll go and to that than, thanks again :)
VulpesPosted May 6, 2011, 9:05 AM
Also, although it doesn't actually do any harm, you don't need to pass the MemoryStream as a 'ref' parameter as you're only changing the existing stream, not assigning a new one.
When I modeled the code using the following console application, it worked fine.
Incidentally, I've had to change the first parameter to Operation_2 to a MemoryStream (probably what you intended) as TextReader doesn't have a Seek() method:
I don't really think there is a more elegant way of doing it apart from saving the intermediate results to disk after each operation for which, of course, you already have code.