本文介绍了" System.IO.BinaryReader.ReadString()"方法应该重命名为ReadDotNetString() - 这是一条多么欺骗性的道路!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哇。我今天玩得很开心。我一直使用这个方法,System.IO.BinaryReader.ReadString()包裹着一个System.IO.FileStream,认为我可以将文件的每一行读成一个字符串,因为FileStream的每一行都将被描述为
by by / n,/ r或/ r / n。因为,它实际上是IS,在我今天使用的源文件中。

Wow. I've had fun with this one today. I kept using this method, System.IO.BinaryReader.ReadString() wrapped around a System.IO.FileStream thinking that I could read each line of the file into a string because each line of the FileStream would be delineated by by /n, /r, or /r/n. Because, it actually IS, in my source file I've been using today.

不是传统的字符串概念,它总是被某些东西终止,无论它是否为null字节(0x00)或回车/换行序列?

Isn't the traditional concept of a string that it is always terminated by something, whether it be a null byte (0x00) or a carriage return/linefeed sequence?

使用.NET Framework多年后,我从梦幻世界走出来,意识到.NET字符串有一个完全不同的方案从来没有在我读过的任何书籍或文章中解释或介绍过。

After many years of using the .NET Framework I come out of my dreamworld and realize that .NET strings have an entirely different scheme going on that was never explained or introduced in any of the books or articles I've ever read.

程序员心目中字符串的一般定义是什么?

What is the general definition of a string in the mind of a programmer?

我想大胆声明System.IO.BinaryReader.ReadString()方法命名非常不合适。除了.NET框架之外,世界上还有谁使用这种确定字符串长度的方案?请将方法重命名为ReadDotNetFrameWorkString()?

I would like to make the bold statement that the System.IO.BinaryReader.ReadString() method is very inappropriately named. Who else in the world uses this scheme of determing a string's length besides the .NET framework? Please rename the method to ReadDotNetFrameWorkString()?

我意识到字符串不等于一行,总是!!但很多时候,它是。可能更多,反之亦然!

I realize a string isn't equal to a line, always!! But a lot of times, it is. Probably more so, than the other way around!

编辑:换句话说,你不能在"任何ole"上使用System.IO.BinaryReader.ReadString()。用于读取"字符串"的二进制文件除非你使用System.IO.BinaryWriter.WriteString();写下"字符串"。

In other words, you can't use System.IO.BinaryReader.ReadString() on just "any ole" binary file to read "strings" unless you used System.IO.BinaryWriter.WriteString(); to write the "strings".

推荐答案

感谢您在此发帖。

>> System.IO.BinaryReader.ReadString()包裹着一个System.IO.FileStream,认为我可以将文件的每个
行读成一个字符串,因为FileStream的每一行都是由/ n,/ r描述的,或者/ r / n。

如果你想读取文件的每一行,你可以尝试使用
File.ReadAllLines
以及读取所有行将文件放入字符串数组中,然后关闭文件。

If you want to read each line of File, you could try to use File.ReadAllLines to read all lines of the file into a string array, and then closes the file.

>> 我想做出一个粗体声明,即System.IO.BinaryReader.ReadString()
方法命名非常不合适。世界上还有谁使用这个方案确定除.NET框架之外的字符串长度?请将方法重命名为ReadDotNetFrameWorkString()?

此方法的字符串长度为在.net框架源代码中定义。 

The string length of this method is defined in .net framework source code. 

 public virtual String ReadString() {
            Contract.Ensures(Contract.Result<String>() != null);
 
            if (m_stream == null)
                __Error.FileNotOpen();
 
            int currPos = 0;
            int n;
            int stringLength;
            int readLength;
            int charsRead;
 
            // Length of the string in bytes, not chars
            stringLength = Read7BitEncodedInt();
            if (stringLength<0) {
                throw new IOException(Environment.GetResourceString("IO.IO_InvalidStringLen_Len", stringLength));
            }
 
            if (stringLength==0) {
                return String.Empty;
            }
 
            if (m_charBytes==null) {
                m_charBytes  = new byte[MaxCharBytesSize];
            }
            
            if (m_charBuffer == null) {
                m_charBuffer = new char[m_maxCharsSize];
            }
            
            StringBuilder sb = null; 
            do
            {
                readLength = ((stringLength - currPos)>MaxCharBytesSize)?MaxCharBytesSize:(stringLength - currPos);
 
                n = m_stream.Read(m_charBytes, 0, readLength);
                if (n==0) {
                    __Error.EndOfFile();
                }
 
                charsRead = m_decoder.GetChars(m_charBytes, 0, n, m_charBuffer, 0);
 
                if (currPos == 0 && n == stringLength)
                    return new String(m_charBuffer, 0, charsRead);
 
                if (sb == null)
                    sb = StringBuilderCache.Acquire(stringLength); // Actual string length in chars may be smaller.
                sb.Append(m_charBuffer, 0, charsRead);
                currPos +=n;
            
            } while (currPos<stringLength);
 
            return StringBuilderCache.GetStringAndRelease(sb);
        }
 




internal protected int Read7BitEncodedInt() {
            // Read out an Int32 7 bits at a time.  The high bit
            // of the byte when on means to continue reading more bytes.
            int count = 0;
            int shift = 0;
            byte b;
            do {
                // Check for a corrupted stream.  Read a max of 5 bytes.
                // In a future version, add a DataFormatException.
                if (shift == 5 * 7)  // 5 bytes max per Int32, shift += 7
                    throw new FormatException(Environment.GetResourceString("Format_Bad7BitInt32"));
 
                // ReadByte handles end of stream cases for us.
                b = ReadByte();
                count |= (b & 0x7F) << shift;
                shift += 7;
            } while ((b & 0x80) != 0);
            return count;
        }

这是.net框架源代码的链接供您参考。

Here is the link of .net framework source code for your reference.

https://referencesource.microsoft.com/#mscorlib/system/io/binaryreader.cs,f30b8b6e8ca06e0f

最好的问候,

Wendy


这篇关于&QUOT; System.IO.BinaryReader.ReadString()&QUOT;方法应该重命名为ReadDotNetString() - 这是一条多么欺骗性的道路!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:21