2016-08-25 20 views
2

小さなテキストファイルがあり、別々の行にいくつかの整数が含まれています。テキストファイルから整数を読み込んでリストに格納する

整数で読み込んでいくつかの変数に割り当てるには、次のプログラム(ちょうどReadFromFileという関数)を書きました。

とどうすればいいですか?私は整数で読むことを試みたが、StreamReaderでエラーが発生することを知ったので、私は文字列を使って続けた。

このプログラムを改善することはできますか?

これはすべて、次の番号で読み込まれ、最初の2つの変数に2つの変数を割り当て、残りをリストに入れます。だから、

3 
4 
8 
8 
8 
8 
8 
8 

、私があります:あなたはヘッダ行を持っている場合var1 = 3を、var2 = 4myList = [8,8,8,8,8,8]

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


namespace Practice 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {  
      // Read the specifications from the file. 
      ReadFromFile(); 

      // Prevent the console window from closing. 
      Console.ReadLine(); 
     } 


     /// The function that reads the input specifications from a file. 
     public static void ReadFromFile() 
     { 
      string localPath = @"C:\Desktop\input.txt"; 
      StreamReader sr = new StreamReader(localPath); 

      // Read all the lines from the file into a list, 
      // where each list element is one line. 

      // Each line in the file. 
      string line = null; 

      // All lines of the file. 
      List<string> lines = new List<string>(); 

      while ((line = sr.ReadLine()) != null) 
      { 
       lines.Add(line); 
       Console.WriteLine(line); 
      }   

      // Display the extracted parameters.     
      Console.WriteLine(lines[0] + " var1"); 
      Console.WriteLine(lines[1] + " var2"); 

      // Put the rest in a separate list. 
      List<int> myList = new List<int>(); 

      for (int i = 2; i < lines.Count; i++) 
      { 
       Console.WriteLine("item {0} = {1}", i-1, lines[i]); 
       myList.Add(Int32.Parse(lines[i])); 
      } 

      sr.Close(); 
     } 
    } 
} 

答えて

3
var vals = File.ReadAllLines(path).Select(int.Parse).ToList(); 

をあなたはSkip(...)が必要になる場合があります。例が一致するためにあなたのfor(int i = 2; ...)

var vals = File.ReadAllLines(path).Skip(2).Select(int.Parse).ToList(); 
+2

オプション:使用int.TryParse安全のための代わりint.Parse内のファイルがへの非数を持っている場合1行に多数。 – BrilBroeder

+1

@BrilBroederは同意しましたが、私はOPのコード(int.Parseを使用しています)と一貫しているようにしようとしていましたが、b:LINQから使用するのはちょっと面倒です:) –

+2

@MarcGravell - OPは ' var1 = 3'、 'var2 = 4'も彼の質問に必要です。 – Enigmativity

3

あなたは次のようにそれを書くことができます:

public static void ReadFromFile(string localPath) // paremetrizing the path for more flexibility 
    { 
     StreamReader sr = new StreamReader(localPath); 

     // extrating the lines from the file 
     List<int> lines = Regex.Split(sr.ReadToEnd(), "\r\n").Select(int.Parse).ToList(); 

     // we can close the reader as we don't need it anymore 
     sr.Close(); 

     Console.WriteLine(lines[0] + " var1"); 
     Console.WriteLine(lines[1] + " var2"); 

     // removing the first 2 elements 
     lines = lines.Skip(2).ToList(); 



     for (int i = 0; i < lines.Count; i++) 
     { 
      Console.WriteLine("item {0} = {1}", i-1, lines[i]); 
     } 

    } 
+0

これはまた、正規表現について学習する必要があることを私に思い出させる。 – Joshua

+1

ここで正規表現の分割は、String.Split()メソッドが1つの文字でのみ分割できるため、ここでは2で分割する必要があるため、 –

関連する問題