2013-10-15 5 views
-4

~ 3 밀 행의 거대한 파일이 있습니다. 모든 행에는 다음과 같은 레코드가 있습니다.파일에서 읽기 및 추출

1|2|3|4|5|6|7|8|9 

'|' 모든 라인에. 이 파일을 읽는 방법을 찾고 나서 모든 줄에서 마지막 '9'숫자를 추출하여 다른 파일에 저장하십시오.

편집 :

여기가 이미 완료되었습니다.

using (StreamReader sr = new StreamReader(filepath)) 
    using (StreamWriter sw = new StreamWriter(filepath1)) 
    { 
     string line = null; 
     while ((line = sr.ReadLine()) != null) 
      sw.WriteLine(line.Split('|')[8]); 
    } 

File.WriteAllLines("filepath", File.ReadAllLines(filepath).Where(l => !string.IsNullOrWhiteSpace(l))); 

파일을 읽고 마지막 자릿수를 추출한 다음 새 파일에 쓰고 빈 줄을 지 웁니다. 마지막 숫자는 10-15 개의 기호이며 처음 6 개를 추출하고 싶습니다. 일부는 계속해서 읽고 시도하며, 끝나면 몇 가지 질문을 다시 편집 할 것입니다.

감사

편집 2 : 이 좋아, 여기에 내가 수에서 처음 8 개 자리를 취할 :

sw.WriteLine(line.Substring(0, Math.Min(line.Length, 8))); 

편집 3 : 나는 아무 생각이 나는 남아 이제 모든 번호와 일치 할 수있는 방법 파일. 나는 그 (것)들을 일치시키고 얼마나 많은 시간이 파일에 있는지 마녀 수를보고 싶 는다.

어떤 도움이 필요합니까?

+10

멋진. 재미있게 보내십시오. 문제가있을 경우 저희에게 알려주십시오. – noelicus

+0

방금 ​​C#으로 발전하지 않아서 몇 가지 아이디어 나 자습서를 찾고 시작했습니다. 고맙습니다. – Goro

+0

행운을 빈다. 한 번에 한 단계 씩 진행하십시오. 작은 작업으로 나누면 행복 할 때 첫 번째 비트 작업이 다음 작업으로 이동합니다. 즐기십시오 :) – noelicus

답변

1
using (StreamReader sr = new StreamReader("input")) 
    using (StreamWriter sw = new StreamWriter("output")) 
    { 
     string line = null; 
     while ((line=sr.ReadLine())!=null) 
      sw.WriteLine(line.Split('|')[8]); 
    } 
+1

이것은 한 줄을 쓸 것입니다 :) – Damith

+1

ㅎ, 생각하지 않고 입력했습니다. :) 나는 지금 그것을 바로 잡았다. –

3

이 파일을 읽는 방법을 찾고 있는데 모든 줄의 마지막 [..] 번호 만 추출하여 다른 파일에 저장하십시오.

정확하게 어떤 부분에 문제가 있습니까?

fileReader = OpenFile("input") 
fileWriter = OpenFile("output") 

while !fileReader.EndOfFile 
    line = fileReader.ReadLine 
    records[] = line.Split('|') 
    value = records[8] 
    fileWriter.WriteLine(value) 
do 

그래서 그것을 구현 시작하고 문제가 발생한 특정 줄에 질문을 주시기 : 사이비 코드에서, 이것은 당신이 원하는 것입니다. 내가 게시 한 각 코드 행에는 C# 코드 또는 웹 검색을 수행하는 용어를 파악할 수있는 충분한 정보가 들어 있습니다.

+0

좋아요! 출발점 주셔서 감사합니다! – Goro

1

을 사용하여 배열 내부의 줄을 가져 와서 마지막 요소를 가져 와서 다른 파일에 저장하십시오. 각 행에 대해 프로세스를 반복하십시오.

2

어디서 붙어 있는지 말하지 마십시오. 문제를 해결하십시오 :

Write and run minimal C# program 

Read lines from file 

Break up one line 

write result line to a file 

그 중 하나에 붙어 있습니까? 그런 다음 그것에 대해 구체적인 질문을하십시오. 이 분해 기술은 많은 프로그래밍 작업의 핵심이며 실제로는 일반적으로 복잡한 작업입니다.

string split 기능이 유용 할 수 있습니다.

var numbers = from l in File.ReadLines(fileName) 
       let p = l.Split('|') 
       select p[8]; 

을 한 후 같은 새로운 파일로 쓰기 :

1

는 LINQ 사용하면 숫자를 필터링하기 위해 다음과 같은 일을 할 수

File.WriteAllText(newFileName, String.Join("\r\n", numbers)); 
+0

참고로 ~ 3 mill 행이 있습니다! – Damith

+0

문제가되지 않습니다. File.ReadLines()는 게으른 방식으로 IEnumerable을 반환합니다. – Flea777

+1

~ 3 mill 레코드로'String.Join'을 부를 때 어떤 일이 일어날 지 궁금합니다. – Damith

1

이보십시오. ..

// Read the file and display it line by line. 
System.IO.StreamReader file = 
    new System.IO.StreamReader("c:\\test.txt"); 
while((line = file.ReadLine()) != null) 
{ 
    string[] words = s.Split('|'); 
    string value = words [8] 
    Console.WriteLine (value); 

} 

file.Close(); 
1

거대한 파일을 한 줄씩 읽어야합니다!

public IEnumerable ReadFileIterator(String filePath) 
     { 
      using (StreamReader streamReader = new StreamReader(filePath, Encoding.Default)) 
      { 
       String line; 
       while ((line = streamReader.ReadLine()) != null) 
       { 
        yield return line; 
       } 
       yield break; 
      } 
     } 

     public void WriteToFile(String inputFilePath, String outputFilePath) 
     { 
      using (StreamWriter streamWriter = new StreamWriter(outputFilePath, true, Encoding.Default)) 
      { 
       foreach (String line in ReadFileIterator(inputFilePath)) 
       { 
        String[] subStrings = line.Split('|'); 
        streamWriter.WriteLine(subStrings[8]); 
       } 
       streamWriter.Flush(); 
       streamWriter.Close(); 
      } 
     }