2016-09-19 13 views
0

저는 Streamreader로 반복하고 라인을 읽음으로써 플랫 파일을 파싱합니다.VB .Net Streamreader - 텍스트 파일에서 개행 전에 공백 자르기

모든 것이 잘 작동하지만 각 레코드 끝에있는 필드가 선택적으로 변경되는 요구 사항이 변경되었습니다. 정의에 대해 각 행의 길이를 검증하여 파일의 형식을 잘못 지정했는지 여부를 결정해야합니다.

이로 인해 Streamreader.ReadLine은 마지막 문자 다음과 줄 바꿈 앞에 오는 공백을 제거합니다. 12345 \ n BOB JONES \ n

에서는 StreamReader는 readline과 및 ReadToEnd 저장소 모두 그 공백을 무시

BOB JONES :

공백으로 대체 숫자 다음의 예를 생각해 보자.

의 Readline : 여기서 메모리 결과

"BOB JONES 12345" "BOB 존스"

ReadToEnd :

"BOB JONES 12345"& vbrclf & "밥 존스"

Readblock과 동일하게 처리 한 다음 버퍼 결과를 문자열로 복사합니다.

끝 날짜 필드가 선택 사항이므로 레코드의 유효성을 검사하는 데 다른 접근 방식을 취할 것입니다. 그러나 제 질문은 Streamreader가 이러한 끝나는 공백을 삭제하는 이유입니까? 그리고 내가 필요하다면 어떻게 읽습니까?

+1

나는이 동작에 너무 놀랐다. 그래서 그것을 테스트했다. 최소한 기본적으로 나에게 StreamReader.ReadLine은이를 수행하지 않습니다. 코드 트리밍에 다른 것이 없다고 확신합니까? 작은 코드 샘플에서 동작을 재현 할 수 있습니까? 사용중인 .Net 버전을 확인할 수 있습니까? – tolanj

답변

0

쉽게이

Imports System.IO 

Module Module1 

    Sub Main() 
     Dim sr As StreamReader = New StreamReader("SampleTextFile.txt") 
     Dim text As String = sr.ReadToEnd 

     Console.WriteLine("Original text") 
     Console.WriteLine(text) 
     Console.WriteLine() 

     Console.WriteLine("White-spaces as .") 
     Console.WriteLine(text.Replace(" ", ".")) 
     Console.WriteLine() 

     Console.ReadKey() 
    End Sub 

End Module 

이 출력

발생 조건이

Some text with 2 white-spaces at the end 
Some other text with one white-space at the end 
No whit-espace at the end 
Next line will be made of white-spaces 

The EN 

같은 대응 SampleTextFile.txt 같은 샘플 프로그램으로 볼 수있는 화이트 스페이스를 트리밍 할 것이다 StreamReader

Original text 
Some text with 2 white-spaces at the end 
Some other text with one white-space at the end 
No whit-espace at the end 
Next line will be made of white-spaces 

The END 

White-spaces as . 
Some.text.with.2.white-spaces.at.the.end.. 
Some.other.text.with.one.white-space.at.the.end. 
No.whit-espace.at.the.end 
Next.line.will.be.made.of.white-spaces 
......... 
The.END 

프로를 확인하고 싶을 수 있습니다. 다시 문자열을 다듬을 수있는 그램.

+0

정확합니다. 더 이른 지점에서 잘라냅니다. 감사! – crayfin

+0

듣기 좋은데 - 답변을 수락하십시오 (녹색 확인 표시를 통해) - thx – DAXaholic