C# 文件读取错误
我正在开发一个文本压缩程序,但是在读取文件时遇到了问题。在获得文件的字节[]后,我将其编码为ASCII(用于我正在测试的.txt文件),然后将字符串打印到屏幕上。我得到的结果是不完整的,显示的字符很少,文档的文本不可读。我已经能够在文本编辑器中成功打开该文件,因此我断定它没有损坏。我希望这个问题对其他人也有帮助。下面是我的相关代码和结果片段 代码: 结果片段:C# 文件读取错误,c#,macos,encoding,C#,Macos,Encoding,我正在开发一个文本压缩程序,但是在读取文件时遇到了问题。在获得文件的字节[]后,我将其编码为ASCII(用于我正在测试的.txt文件),然后将字符串打印到屏幕上。我得到的结果是不完整的,显示的字符很少,文档的文本不可读。我已经能够在文本编辑器中成功打开该文件,因此我断定它没有损坏。我希望这个问题对其他人也有帮助。下面是我的相关代码和结果片段 代码: 结果片段: 结果中的区域包括'(?)是文件中文本应该显示的位置。读取文本文件有理由这样做吗?StreamReaders呢?我也尝试过使用Strea
结果中的区域包括'(?)是文件中文本应该显示的位置。读取文本文件有理由这样做吗?StreamReaders呢?我也尝试过使用StreamReader,但结果相同如果用记事本打开文件,你会看到什么?它会显示星型系统的名称,这不是ASCII文件
//Takes the file and breaks it down into a byte array
public void ReadFile(string fileLocation) {
FileStream file = new FileStream(@fileLocation, FileMode.Open); //Opens the file
byte[] buffer = new byte[file.Length]; //Sets the Length of the array 'buffer' to the length of the amound of bytes in the file
file.Read(buffer, 0, (int)file.Length); //Reads the data (bytes) from the file and stores it in 'buffer'
file.Close(); //Closes the file
Convert(buffer); //Starts the function 'Convert' passing in 'buffer' as the dada value
}
//Takes the byte array and puts it into ASCII encoding
public void Convert(byte[] byteData) {
data = Encoding.ASCII.GetString(byteData); //Convert and store the converted data into the variable 'data'
}