LoginSignup
0
1

More than 3 years have passed since last update.

C# 勉強(4) ファイル入力2

Last updated at Posted at 2020-05-25

テキストファイルを読み込んで、特定店舗の行のみ出力する。

:Program.cs
using System;
using System.IO;
using System.Text;

namespace List094
{
    class Program
    {
        static void Main(string[] args)
        {
        //    var filePath = @"C:\Example\Greeting";
            string filePath = "Sales.txt";
            string[] lines = File.ReadAllLines(filePath,Encoding.UTF8);
            foreach(var line in lines){
                string[] items = line.Split(',');
                Sale sale =new Sale{
                    ShopName = items[0],
                    Product =items[1],
                    Amount =int.Parse(items[2])
                };
                if(sale.ShopName=="浅草店")
                    Console.WriteLine("{0}  {1}  {2}",sale.ShopName,sale.Product,sale.Amount);
            }
        }
    }
}

クラス

:Sales.cs
using System;

namespace List094 {
    public class Sale{
        public String ShopName { get; set; }
        public String Product { get; set; }
        public int Amount { get; set; }
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1