challenge 条件を満たす行を取り除く

ファイルから1行ずつ読み込み、"#"で始まる行だけを取り除いてファイルに出力するコードを書いてください。

サンプル入力

hello!
# remove this
 # don't remove this
bye!
サンプル出力
hello!
 # don't remove this
bye!

Posted feedbacks - C#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using System;
class Program
{
  static void Main()
  {
    string s;
    while ((s = Console.ReadLine()) != null)
      if (!s.StartsWith("#")) Console.WriteLine(s);
  }
}

たこのお題の趣旨は「ファイルハンドルの扱い」×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);
                        }
                    }
                }
            }
        }
    }
}

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);
                    }
                }
            }
        }
    }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var lines =
            from s in File.ReadAllLines("infile.txt")
            where !s.StartsWith("#")
            select s;

        File.WriteAllLines("outfile.txt", lines.ToArray());
    }
}

Index

Feed

Other

Link

Pathtraq

loading...