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.");
}
FileStream은 "보이지 않는"문자를 무시하지 않으며 문자를 신경 쓰지 않습니다. 'StreamReader'는 텍스트를 읽고 줄 바꿈을 처리하는 클래스입니다.하지만 무시하지는 않습니다. 두 경우 모두 파일이 * 같은 *입니까? * 내용 *은 동일합니까? 문제를 재현하는 코드를 게시 할 수 있습니까? –
@PanagiotisKanavos 분명히 나는 X Y 문제가 있어야합니다. 네, 물론 할 수 있습니다. 물어봐 줘서 고마워. 희망이 여기에 무슨 일이 일어나고 있는지 알아낼 수 있습니다. – Snoopy
Windows에서이 코드를 .NET 4.6.2에서 문제를 재현 할 수 없습니다.'File.Open'과'File.OpenRead' 둘 다'FileStream' 생성자를 둘러싼 단순한 래퍼이기 때문에 두 코드 모두 첫 번째 빈 줄까지 개별적으로 줄을 만듭니다. –