2013-02-28 9 views
-4

텍스트 파일의 내용에서 여섯 개의 배열을 만들려면 어떻게 텍스트 파일을 반복 할 수 있습니다. 예를 들어, 텍스트 파일은 다음과 같이하지만 더 라인 (제목 없음) 그배열로 텍스트 파일을 읽는 C#

top_speed average_speed cadence altitude heart_rate power 

     84   73  0  -124   0 50 
     86   179  84 -125  121 3893 

의 아마 200 각 배열을 가지고 좋은 것으로 볼 것이다. 따라서, 예를 들어

top_speed = 84 + 86 : average_speed = 73 + 179 ...

이 작업을 수행하는 가장 좋은 방법은 무엇입니까 (등)?

+8

내가 숙제를 냄새 ... 내가 배열을 냄새 –

+2

이 ... –

+0

당신은 무엇을 시도? 당신이 찾고있는 결과를 얻을 것이라고 생각하는 접근 방법은 무엇입니까? – Tim

답변

0

어쨌든

var items = 
    File.ReadAllLines(filename) // read lines from file 
     .Select(line => line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries) 
          .Select(Int32.Parse) 
          .ToArray()) // convert each line to array of integers 
     .Select(values => new { 
      TopSpeed = values[0], 
      AverageSpeed = values[1], 
      Cadence = values[2], 
      Altitude = values[3], 
      HeartRate = values[4], 
      Power = values[5] 
     }); // create anonymous object with nice strongly-typed properties 

int[] topSpeeds = items.Select(i => i.TopSpeed).ToArray(); 
0

당신은 Record 클래스를 만들 수있는 숙제 경우, 코드를 다음은 도움이되지 않습니다 :)하지만 숙제를하지 않은 경우, 당신은 LINQ와 같은 파일을 구문 분석하는 방법을 이해하고 간단한 LINQ 쿼리를 사용하십시오.

var records = File.ReadLines("file.txt") 
    .Select(line => 
     { 
      string[] parts = line.Split('\t'); 
      return new Record 
       { 
        TopSpeed = int.Parse(parts[0]), 
        AverageSpeed = int.Parse(parts[1]), 
        Cadence = int.Parse(parts[2]), 
        Altitude = int.Parse(parts[3]), 
        HeartRate = int.Parse(parts[4]), 
        Power = int.Parse(parts[5]) 
       }; 
     }).ToArray(); 

이렇게하면 원래 파일의 한 줄에 하나씩 레코드 집합을 얻을 수 있습니다. 그런 다음 히스토그램 또는 그래프 또는 무엇이든을 구축하기위한 HeartRates을 모두 확인하고 싶었다면,이처럼 그들을 잡아 수 :

var allHeartRates = records.Select(rec => rec.HeartRate); 
관련 문제