아래 데이터 형식의 LOG.TXT 에서 읽어서 

[2024-06-11 오전 12:32:05] - 1 2
[2024-06-11 오전 12:32:06] - 3 4
[2024-06-11 오전 12:32:07] - 5 6
[2024-06-11 오전 12:32:08] - 7 8
[2024-06-11 오전 12:32:09] - 9 10
[2024-06-11 오전 12:32:10] - 11 12
[2024-06-11 오전 12:32:11] - 13 14
[2024-06-11 오전 12:32:12] - 15 16
[2024-06-11 오전 12:32:13] - 17 18
[2024-06-11 오전 12:32:14] - 19 20
[2024-06-11 오전 12:32:15] - 21 22
[2024-06-11 오전 12:32:16] - 23 24
[2024-06-11 오전 12:32:17] - 25 26
[2024-06-11 오전 12:32:18] - 27 28
[2024-06-11 오전 12:32:19] - 29 30

 

아래와 같이 두 수를 더해서 결과값을 읽음 

[2024-06-11 오전 12:34:48] 3
[2024-06-11 오전 12:34:48] 7
[2024-06-11 오전 12:34:48] 11
[2024-06-11 오전 12:34:48] 15
[2024-06-11 오전 12:34:48] 19
[2024-06-11 오전 12:34:48] 23
[2024-06-11 오전 12:34:48] 27
[2024-06-11 오전 12:34:48] 31
[2024-06-11 오전 12:34:48] 35
[2024-06-11 오전 12:34:48] 39
[2024-06-11 오전 12:34:48] 43
[2024-06-11 오전 12:34:48] 47
[2024-06-11 오전 12:34:48] 51
[2024-06-11 오전 12:34:48] 55
[2024-06-11 오전 12:34:49] 59

 

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

class Program
    {
        static void Main(string[] args)
        {
            while (!File.Exists("LOG.TXT"))
            {
                Thread.Sleep(100);
                continue;
            }

            FileStream fs = new FileStream("LOG.TXT", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            while (true)
            {
                string line = sr.ReadLine();
                if (line == null)
                {
                    Thread.Sleep(1);
                    continue;
                }

#if 0 //아래는 다른 표현방법
                string[] words = line.Split(new[] { " - " }, StringSplitOptions.None)[1].Split(' ');
               int sum = int.Parse(words[0]) + int.Parse(words[1]);
#else
              string[] words = line.Split(" - ");
               string command = words[0];
               string[] nums = words[1].Split(' ');
              int sum = int.Parse(nums[0]) + int.Parse(nums[1]);
#endif 
               
                Console.WriteLine("[{0}] {1}", DateTime.Now, sum);
                Thread.Sleep(1);

 
            }
            sr.Close();
        }
    }

+ Recent posts