Simultaneous reading and recording of the file



  • There are two text files. 1 flow reads in rows from file (1) and reports to file (2), 2 flow on line reads the infu from file (2) and reports it to file (1). In fact, it is necessary that the programme be carried out indefinitely, as it will not be possible to complete the files. But now the streamReader reads the files as if they're not changing in the course of the programme, even though it has completed the files with an info another one time for each file. That's my code now. What could be a mistake?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.IO;
    

    namespace l6_1
    {

    class Program
    {
    public static string file1 = "D:/l6sharp/1.txt";
    public static string file2 = "D:/l6sharp/2.txt";
    private static FileStream filestream1;
    private static FileStream filestream2;
    private static StreamReader f1r;
    private static StreamReader f2r;
    private static StreamWriter f1w;
    private static StreamWriter f2w;

    static void from1to2()
    {
        string line;
        while (true)
        {
    
            if ((line = f1r.ReadLine()) != null)
            {
                System.Console.WriteLine(line);
                f2w.WriteLine(line);
                f2w.Flush();
            }
            Thread.Sleep(100);
        }
    }
    static void from2to1()
    {
        string line;
        while (true)
        {
    
            if ((line = f2r.ReadLine()) != null)
            {
                System.Console.WriteLine(line);
                f1w.WriteLine(line);
                f1w.Flush();
            }
            Thread.Sleep(100);
        }
    }
    static void Main(string[] args)
    {
        filestream1 = new System.IO.FileStream(file1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        filestream2 = new System.IO.FileStream(file2, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        f1r = new StreamReader(filestream1);
        f2r = new StreamReader(filestream2);
        f1w = new StreamWriter(filestream1);
        f2w = new StreamWriter(filestream2);
        Task task1 = new Task(from1to2);
        Task task2 = new Task(from2to1);
        task1.Start();
        task2.Start();
        Console.ReadLine();
    }
    

    }
    }



  • As VladD noted,

    StreamReader is likely to buffer the record. So if The size of the file is smaller than the buffer, he's StreamReader just at the beginning. Thinks the whole file is in memory, that's all.

    Andrew:

    On reading and recording, the index of the main stream of the file was not changed, As expected. Added to the continuation of the position with which the Reading, now the tissue works as it should.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2