2016-10-26 6 views
-1

C#의 파일 스트림에서 데이터를 읽으려고 시도한 두 개의 코드 블록이 있습니다. 여기 내 전체 목표는 텍스트의 각 줄을 문자열 목록으로 읽는 것입니다. 그러나 모두 단일 문자열로 읽습니다 (읽기 + 쓰기 액세스 함께 열었을 때)왜 FileStream은 때때로 보이지 않는 문자를 무시합니까?

나는 오전 첫 번째 코드 블록이 캐리지 리턴과 줄 바꿈 모두를 올바르게 읽음을 알리고 다른 한 줄은 무시합니다. 나는 정말로 여기서 무슨 일이 일어나고 있는지 확신하지 못한다. 스트림을 두 가지 방법으로 열어 봤지만 그건 중요하지 않아요? 음, 여기 어떤 경우에도 코드의 첫 번째 블록 (즉, 제대로 읽고 - 내 공백 문자)입니다 : 이제

StreamReader sr = null; 
StreamWriter sw = null; 
FileStream fs = null; 
List<string> content = new List<string>(); 
List<string> actual = new List<string>(); 
string line = string.Empty; 

// first, open up the file for reading 
fs = File.OpenRead(path); 
sr = new StreamReader(fs); 

// read-in the entire file line-by-line 
while(!string.IsNullOrEmpty((line = sr.ReadLine()))) 
{ 
    content.Add(line); 
} 
sr.Close(); 

는, 여기에 공백 문자를 모두 (무시 코드 블록이다 줄 바꿈, 캐리지 리턴)을 사용하고 한 줄로 전체 파일을 읽습니다.

StreamReader sr = null; 
StreamWriter sw = null; 
FileStream fs = null; 
List<string> content = new List<string>(); 
List<string> actual = new List<string>(); 
string line = string.Empty; 

// first, open up the file for reading/writing 
fs = File.Open(path, FileMode.Open); 
sr = new StreamReader(fs); 

// read-in the entire file line-by-line 
while(!string.IsNullOrEmpty((line = sr.ReadLine()))) 
{ 
    content.Add(line); 
} 
sr.Close(); 

Open 원인 모든 데이터가 하나의 라인으로 읽을 수 않는 이유 OpenRead는 (여러 라인 데이터를 읽어) 제대로 작동?

UPDATE 1

나는 문제가 재생 파일의 텍스트를 제공하도록 요청하고있다. 그래서 여기가 (가 여기에 붙여 얻을 것이다 경우에 나는 확실하지 않다! CR + LF 각 줄의 끝 부분에 있는지 확인!) 다음과 같습니다

;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
;$$$$$$$$$                $$$$$$$ 
;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
; 
; 
; 

업데이트 2

파일을 위의 텍스트를 사용하여 문제를 재현하는 코드 블록입니다. 이 경우 실제로는 Open을 시도하지 않고 단지 OpenRead을 사용하여 문제를보고 있습니다.

StreamReader sr = null; 
StreamWriter sw = null; 
FileStream fs = null; 
List<string> content = new List<string>(); 
List<string> actual = new List<string>(); 
string line = string.Empty; 

try 
{ 
    // first, open up the file for reading/writing 
    fs = File.OpenRead(path); 
    sr = new StreamReader(fs); 

    // read-in the entire file line-by-line 
    while(!string.IsNullOrEmpty((line = sr.ReadLine()))) 
    { 
     content.Add(line); 
    } 
    sr.Close(); 

    // now, erase the contents of the file 
    File.WriteAllText(path, string.Empty); 

    // make sure that the contents of the file have been erased 
    fs = File.OpenRead(path); 
    sr = new StreamReader(fs); 
    if (!string.IsNullOrEmpty(line = sr.ReadLine())) 
    { 
     Trace.WriteLine("Failed: Could not erase the contents of the file."); 
     Assert.Fail(); 
    } 
    else 
    { 
     Trace.WriteLine("Passed: Successfully erased the contents of the file."); 
    } 

    // now, attempt to over-write the contents of the file 
    fs.Close(); 
    fs = File.OpenWrite(path); 
    sw = new StreamWriter(fs); 
    foreach(var l in content) 
    { 
     sw.Write(l); 
    } 

    // read back the over-written contents of the file 
    fs.Close(); 
    fs = File.OpenRead(path); 
    sr = new StreamReader(fs); 
    while (!string.IsNullOrEmpty((line = sr.ReadLine()))) 
    { 
     actual.Add(line); 
    } 

    // make sure the contents of the file are correct 
    if(content.SequenceEqual(actual)) 
    { 
     Trace.WriteLine("Passed: The contents that were over-written are correct!"); 
    } 
    else 
    { 
     Trace.WriteLine("Failed: The contents that were over-written are not correct!"); 
    } 
} 
finally 
{ 
    // close out all the streams 
    fs.Close(); 

    // finish-up with a message 
    Trace.WriteLine("Finished running the overwrite-file test."); 
} 
+1

FileStream은 "보이지 않는"문자를 무시하지 않으며 문자를 신경 쓰지 않습니다. 'StreamReader'는 텍스트를 읽고 줄 바꿈을 처리하는 클래스입니다.하지만 무시하지는 않습니다. 두 경우 모두 파일이 * 같은 *입니까? * 내용 *은 동일합니까? 문제를 재현하는 코드를 게시 할 수 있습니까? –

+0

@PanagiotisKanavos 분명히 나는 ​​X Y 문제가 있어야합니다. 네, 물론 할 수 있습니다. 물어봐 줘서 고마워. 희망이 여기에 무슨 일이 일어나고 있는지 알아낼 수 있습니다. – Snoopy

+0

Windows에서이 코드를 .NET 4.6.2에서 문제를 재현 할 수 없습니다.'File.Open'과'File.OpenRead' 둘 다'FileStream' 생성자를 둘러싼 단순한 래퍼이기 때문에 두 코드 모두 첫 번째 빈 줄까지 개별적으로 줄을 만듭니다. –

답변

0

새 파일 끝 (end-of-line) 문자가 content에 포함되지 않기 때문에 줄 끝 문자를 포함하지 않는

foreach(var l in content) 
{ 
    sw.Write(l); 
} 

에 의해 생성.

@DaveKidder가 this thread over here으로 지적한대로 spec for StreamReader.ReadLine은 결과 라인에 줄의 끝이 포함되어 있지 않다고 말합니다.

당신이

while(!string.IsNullOrEmpty((line = sr.ReadLine()))) 
{ 
    content.Add(line); 
} 
sr.Close(); 

을 수행 할 때 당신은 라인 문자의 끝을 잃어 가고있다.

+0

두 번째로 나는 그것을 실제로 읽으려고 노력하지만, 처음에는 그것을 읽는 예제에서 읽습니다. – Snoopy

+0

@StevieV 15 번째 줄에서 17 번째 줄을 처음 읽을 때 응용 프로그램 외부에서 줄 끝 기호로 작성된 파일을 읽는 것입니다. 두 번째로 50 행에서 53 행까지 읽으면 응용 프로그램에서 줄 끝 기호없이 작성된 파일을 읽는 것입니다. 이것은 읽는 방법과 아무 상관이 없습니다. 처음 data1을 읽었을 때, 두 번째로 data2를 읽었을 때 data1이 data2와 다른 바이트를 가지고있을 때. 11 번째 줄 이후에 바이트 배열을 저장 한 다음 48 번째 줄 이후에 동일한 작업을 수행하여 검사하면 바이트 세트가 달라집니다. – Nomenator