「C Sharpの基礎 - CSVファイル」の版間の差分

ナビゲーションに移動 検索に移動
227行目: 227行目:
     static void Main()
     static void Main()
     {
     {
      // 読み込み処理
      ReadCSV("input.csv");
       // 書き込み処理
       // 書き込み処理
       WriteCSV("output.csv");
       WriteCSV("sample.csv");
    }
    static void ReadCSV(string inputFile)
    {
      using (var parser = new TextFieldParser(inputFile, Encoding.GetEncoding("Shift_JIS")))
      {
          parser.TextFieldType = FieldType.Delimited;
          parser.Delimiters = new[] {","};
          parser.CommentTokens = new[] {"#"};
          while (!parser.EndOfData)
          {
            try
            {
                string[] row = parser.ReadFields();
                foreach (string field in row)
                {
                  string processedField = field.Replace("\r\n", "n").Replace(" ", "_");
                  Console.Write(processedField + "\t");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("読み込みエラー: " + ex.Message);
            }
          }
      }
     }
     }
   
   

案内メニュー