Comment detail

条件を満たす行を取り除く (Nested Flatten)
たこのお題の趣旨は「ファイルハンドルの扱い」×2だと思うので忠実にやってみました。

C#なのでCloseはusingに任せるが吉♪
ただしハンドルが2つの場合にこの書き方で良いのか不安(^^;
むしろ他の言語でusingの代わりにどう実装するのか見てみたいです。
(たぶんJavaならfinallyだと思うけど…)

正規表現は趣旨でないと思ったので割愛ッ!

注意:このコードを試すときは実行ディレクトリにファイルを用意するかフルパスを指定して下さい。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Text;

namespace FileIO
{
    class Program
    {
        static void Main(string[] args)
        {
            FileIO("sample_in.txt", "sample_out.txt");
        }

        static void FileIO(string fnameIn, string fnameOut)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fnameOut,false,System.Text.Encoding.Default))
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(fnameIn, System.Text.Encoding.Default))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (! line.StartsWith("#"))
                        {
                            sw.WriteLine(line);
                        }
                    }
                }
            }
        }
    }
}
まんまF#でやってみました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
open System;;
open System.Text;;
open System.IO;;
open Array;;

let fileIO (ifname:string) (ofname:string) =
    let streamW = new StreamWriter( ofname, false, Encoding.Default ) in
    let streamR = new StreamReader( ifname, Encoding.Default ) in
    let rmCommentLn (wstream:StreamWriter) =
        let rec readf (rstream:StreamReader) =
            let line = rstream.ReadLine() in
            if line = null then ()
            else
                begin
                    if not (line.StartsWith "#")
                    then wstream.WriteLine line
                    else ();
                    readf rstream
                end
        in using streamR readf
    in using streamW rmCommentLn;;

if (length Sys.argv) = 3
then fileIO Sys.argv.(1) Sys.argv.(2)
else ();;
usingは多段重ねできるんですね(^^;
http://www.divakk.co.jp/aoyagi/csharp_tips_using.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
using System.Text;

namespace FileIO
{
    class Program
    {
        static void Main(string[] args)
        {
            FileIO("sample_in.txt", "sample_out.txt");
        }

        static void FileIO(string fnameIn, string fnameOut)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fnameOut,false,System.Text.Encoding.Default))
            using (System.IO.StreamReader sr = new System.IO.StreamReader(fnameIn, System.Text.Encoding.Default))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (!line.StartsWith("#"))
                    {
                        sw.WriteLine(line);
                    }
                }
            }
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...