wouterk

wouterk

  • NA
  • 13
  • 30.7k

Using one memorystream for multiple file search iterations

May 6 2011 4:26 AM
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).


Answers (3)