写入后获取准确的流位置
Getting Exact Stream Position After Writing
在使用 StreamWriter.Write()
后检查 FileStream.Position
,我在给定值中遇到了一些不准确,我根据生成的文件和使用的字符串的长度进行了验证:
string MyText = "\"aaqnt - bra\"\t\"kkaaknc qzxukrnt\"\t\"kkaaknc qzx xrrqtrq zn\"\t\"arhkxar kq Nz.\"\t\"qkstrkkutkzn xhqnnra\"\t\"knazkxr xqtrczra\"\t\"arhkxar Nrt arkxr\"\t\"qkzNrtarkxr\"\t\"TztaqTqkzunt\"\t\"aqT %\"\t\"Nrtknazkxrqkzunt\"\r\n\"8009\"\t\"9000099989\"\t\"9098-09-99\"\t\"kHbkF09F9Hb009890\"\t\"09\"\t\"k\"\t\"09,000\"\t\"\"\t\"9,000\"\t\"0.00%\"\t\"00,000\"\r\n\"8008\"\t\"9000099009\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999098\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8008\"\t\"9000099999\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009899\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8008\"\t\"9000090988\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999098\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000090990\"\t\"9098-09-99\"\t\"JTNk99Hb9J0090909\"\t\"09\"\t\"k\"\t\"99,999\"\t\"\"\t\"9,899.90\"\t\"0.00%\"\t\"99,090.90\"\r\n\"8000\"\t\"9000090099\"\t\"9098-09-99\"\t\"kR0xW8kk8J0099900\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,900\"\t\"0.00%\"\t\"09,000\"\r\n\"8000\"\t\"9000090900\"\t\"9098-09-99\"\t\"JTNk99Hb9J0099098\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000090000\"\t\"9098-09-99\"\t\"kHFk99F00J9099009\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8000\"\t\"9000090090\"\t\"9098-09-99\"\t\"kHFxX8rk9J0909990\"\t\"09\"\t\"k\"\t\"88,900\"\t\"\"\t\"9,080\"\t\"0.00%\"\t\"99,980\"\r\n\"8000\"\t\"9000090900\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0990099\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000099090\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999098\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8009\"\t\"9000098098\"\t\"9098-09-09\"\t\"Rbakk9Hr9J0999889\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8009\"\t\"9000099999\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009908\"\t\"09\"\t\"k\"\t\"99,999\"\t\"\"\t\"9,899.90\"\t\"0.00%\"\t\"99,090.90\"\r\n\"8009\"\t\"9000090099\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0999888\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8009\"\t\"9000090989\"\t\"9098-09-99\"\t\"9T9kb9rk9HU989890\"\t\"09\"\t\"k\"\t\"909,900\"\t\"\"\t\"9,800\"\t\"0.00%\"\t\"999,000\"\r\n\"8009\"\t\"9000090009\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0909899\"\t\"09\"\t\"k\"\t\"89,999\"\t\"\"\t\"0,899.90\"\t\"0.00%\"\t\"80,990.90\"\r\n\"8009\"\t\"9000090989\"\t\"9098-09-99\"\t\"JTNk99Hb0J0090989\"\t\"09\"\t\"k\"\t\"89,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"88,090\"\r\n\"8000\"\t\"9000099009\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0990900\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000099909\"\t\"9098-09-99\"\t\"Rbakk9Hr0J0909909\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8008\"\t\"9000090990\"\t\"9098-09-99\"\t\"JTNk99Hb9J0090999\"\t\"09\"\t\"k\"\t\"89,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"88,090\"\r\n\"8008\"\t\"9000090099\"\t\"9098-09-99\"\t\"JTkRF9ra0Jq998990\"\t\"09\"\t\"k\"\t\"900,900\"\t\"\"\t\"0,000\"\t\"0.00%\"\t\"900,900\"\r\n\"8099\"\t\"9000090009\"\t\"9098-09-99\"\t\"JTNk99HbXJ0090909\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000099080\"\t\"9098-09-99\"\t\"kHFk99F09J9099999\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8099\"\t\"9000090999\"\t\"9098-09-99\"\t\"kHFk99F0XJ9098080\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8090\"\t\"9000099999\"\t\"9098-09-99\"\t\"Rbakk9HrXJ0990099\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8090\"\t\"9000099900\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0998988\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000098899\"\t\"9098-09-90\"\t\"Rbakk9Hr8J0999988\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000099000\"\t\"9098-09-99\"\t\"kHFk99F09J9090889\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8098\"\t\"9000090990\"\t\"9098-09-99\"\t\"kHFxX8cS9J0990980\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,980\"\t\"0.00%\"\t\"909,980\"\r\n\"8098\"\t\"9000090099\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0999080\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8098\"\t\"9000099999\"\t\"9098-09-99\"\t\"JTNk99Hb9J0008890\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8098\"\t\"9000099099\"\t\"9098-09-99\"\t\"kHFk99F00J9099990\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8098\"\t\"9000099099\"\t\"9098-09-99\"\t\"Rbakk9Hr0J0999998\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8098\"\t\"9000099990\"\t\"9098-09-99\"\t\"Rbakk9HrXJ0999099\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000098899\"\t\"9098-09-90\"\t\"JTNk99Hb9J0090990\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8080\"\t\"9000099999\"\t\"9098-09-99\"\t\"kHFk99F08J9090899\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8089\"\t\"9000099099\"\t\"9098-09-90\"\t\"Rbakk9Hr9J0999090\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8080\"\t\"9000099099\"\t\"9098-09-99\"\t\"JTNk99Hb9J0090989\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8088\"\t\"9000099090\"\t\"9098-09-90\"\t\"JTNk99Hb9J0090009\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8088\"\t\"9000099009\"\t\"9098-09-99\"\t\"JTNk99HbXJ0090900\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8088\"\t\"9000090999\"\t\"9098-09-99\"\t\"JTNk99Hb0J0090998\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8090\"\t\"9000099999\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999889\"\t\"09\"\t\"k\"\t\"89,999\"\t\"\"\t\"0,899.90\"\t\"0.00%\"\t\"80,990.90\"\r\n\"8900\"\t\"9000098000\"\t\"9098-09-09\"\t\"JTNk99HbXJ0090809\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000099099\"\t\"9098-09-90\"\t\"kHFk99F08J9099990\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8900\"\t\"9000099899\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009808\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000099809\"\t\"9098-09-99\"\t\"JTNk99Hb9J0000000\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000099890\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009990\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000090089\"\t\"9098-09-99\"\t\"JTNk99Hb9J0000899\"\t\"09\"\t\"k\"\t\"89,999\"\t\"\"\t\"9,999.90\"\t\"0.00%\"\t\"89,990.90\"\r\n\"8989\"\t\"9000099009\"\t\"9098-09-99\"\t\"kHFkT9F09c9080909\"\t\"09\"\t\"k\"\t\"08,000\"\t\"\"\t\"9,800\"\t\"0.00%\"\t\"08,800\"";
int TextLengthInBytes = Encoding.UTF8.GetByteCount(MyText); // 5127.
FileStream Nonsense = new FileStream("D:\180415_21569.txt", FileMode.CreateNew, FileAccess.Write, FileShare.None);
StreamWriter Insanity = new StreamWriter(Nonsense, Encoding.UTF8);
Insanity.Write(MyText); // Position 0 => 5123.
Insanity.Flush();
Nonsense.Dispose();
对于此示例字符串,差值 (5123) 比正确值 (5127) 小 4。使用我的原始字符串,它减少了 7 (5120)。
我想知道这种差异背后的原因,以及解决它的可能性。 这似乎与默认缓冲无关,因为结果值有时是奇数而不是偶数,而且它在进行微跳跃。
我正在考虑将上面显示的 GetByteCount
作为跟踪真实位置的替代方法,尽管它与实际感兴趣的 point/code 相距甚远,假设值始终相等,并且很可能是完全临时
希望有人对此有所了解。转发谢谢
编辑:
@Daisy + @Jeroen,刚刚了解 BOM,显然我的原始字符串来自无 BOM 文件,而上面的示例当然是添加 BOM,可爱。另外,我认为我也在冲洗后进行了测试,但似乎我没有...
您是在调用 Flush() 之前还是之后获得位置?我认为这就是问题所在。在 Flush() 之前,不能保证所有内容都已写入。
在使用 StreamWriter.Write()
后检查 FileStream.Position
,我在给定值中遇到了一些不准确,我根据生成的文件和使用的字符串的长度进行了验证:
string MyText = "\"aaqnt - bra\"\t\"kkaaknc qzxukrnt\"\t\"kkaaknc qzx xrrqtrq zn\"\t\"arhkxar kq Nz.\"\t\"qkstrkkutkzn xhqnnra\"\t\"knazkxr xqtrczra\"\t\"arhkxar Nrt arkxr\"\t\"qkzNrtarkxr\"\t\"TztaqTqkzunt\"\t\"aqT %\"\t\"Nrtknazkxrqkzunt\"\r\n\"8009\"\t\"9000099989\"\t\"9098-09-99\"\t\"kHbkF09F9Hb009890\"\t\"09\"\t\"k\"\t\"09,000\"\t\"\"\t\"9,000\"\t\"0.00%\"\t\"00,000\"\r\n\"8008\"\t\"9000099009\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999098\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8008\"\t\"9000099999\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009899\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8008\"\t\"9000090988\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999098\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000090990\"\t\"9098-09-99\"\t\"JTNk99Hb9J0090909\"\t\"09\"\t\"k\"\t\"99,999\"\t\"\"\t\"9,899.90\"\t\"0.00%\"\t\"99,090.90\"\r\n\"8000\"\t\"9000090099\"\t\"9098-09-99\"\t\"kR0xW8kk8J0099900\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,900\"\t\"0.00%\"\t\"09,000\"\r\n\"8000\"\t\"9000090900\"\t\"9098-09-99\"\t\"JTNk99Hb9J0099098\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000090000\"\t\"9098-09-99\"\t\"kHFk99F00J9099009\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8000\"\t\"9000090090\"\t\"9098-09-99\"\t\"kHFxX8rk9J0909990\"\t\"09\"\t\"k\"\t\"88,900\"\t\"\"\t\"9,080\"\t\"0.00%\"\t\"99,980\"\r\n\"8000\"\t\"9000090900\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0990099\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000099090\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999098\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8009\"\t\"9000098098\"\t\"9098-09-09\"\t\"Rbakk9Hr9J0999889\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8009\"\t\"9000099999\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009908\"\t\"09\"\t\"k\"\t\"99,999\"\t\"\"\t\"9,899.90\"\t\"0.00%\"\t\"99,090.90\"\r\n\"8009\"\t\"9000090099\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0999888\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8009\"\t\"9000090989\"\t\"9098-09-99\"\t\"9T9kb9rk9HU989890\"\t\"09\"\t\"k\"\t\"909,900\"\t\"\"\t\"9,800\"\t\"0.00%\"\t\"999,000\"\r\n\"8009\"\t\"9000090009\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0909899\"\t\"09\"\t\"k\"\t\"89,999\"\t\"\"\t\"0,899.90\"\t\"0.00%\"\t\"80,990.90\"\r\n\"8009\"\t\"9000090989\"\t\"9098-09-99\"\t\"JTNk99Hb0J0090989\"\t\"09\"\t\"k\"\t\"89,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"88,090\"\r\n\"8000\"\t\"9000099009\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0990900\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8000\"\t\"9000099909\"\t\"9098-09-99\"\t\"Rbakk9Hr0J0909909\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8008\"\t\"9000090990\"\t\"9098-09-99\"\t\"JTNk99Hb9J0090999\"\t\"09\"\t\"k\"\t\"89,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"88,090\"\r\n\"8008\"\t\"9000090099\"\t\"9098-09-99\"\t\"JTkRF9ra0Jq998990\"\t\"09\"\t\"k\"\t\"900,900\"\t\"\"\t\"0,000\"\t\"0.00%\"\t\"900,900\"\r\n\"8099\"\t\"9000090009\"\t\"9098-09-99\"\t\"JTNk99HbXJ0090909\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000099080\"\t\"9098-09-99\"\t\"kHFk99F09J9099999\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8099\"\t\"9000090999\"\t\"9098-09-99\"\t\"kHFk99F0XJ9098080\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8090\"\t\"9000099999\"\t\"9098-09-99\"\t\"Rbakk9HrXJ0990099\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8090\"\t\"9000099900\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0998988\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000098899\"\t\"9098-09-90\"\t\"Rbakk9Hr8J0999988\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000099000\"\t\"9098-09-99\"\t\"kHFk99F09J9090889\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8098\"\t\"9000090990\"\t\"9098-09-99\"\t\"kHFxX8cS9J0990980\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,980\"\t\"0.00%\"\t\"909,980\"\r\n\"8098\"\t\"9000090099\"\t\"9098-09-99\"\t\"Rbakk9Hr8J0999080\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8098\"\t\"9000099999\"\t\"9098-09-99\"\t\"JTNk99Hb9J0008890\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8098\"\t\"9000099099\"\t\"9098-09-99\"\t\"kHFk99F00J9099990\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8098\"\t\"9000099099\"\t\"9098-09-99\"\t\"Rbakk9Hr0J0999998\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8098\"\t\"9000099990\"\t\"9098-09-99\"\t\"Rbakk9HrXJ0999099\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8099\"\t\"9000098899\"\t\"9098-09-90\"\t\"JTNk99Hb9J0090990\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8080\"\t\"9000099999\"\t\"9098-09-99\"\t\"kHFk99F08J9090899\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8089\"\t\"9000099099\"\t\"9098-09-90\"\t\"Rbakk9Hr9J0999090\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"0,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8080\"\t\"9000099099\"\t\"9098-09-99\"\t\"JTNk99Hb9J0090989\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8088\"\t\"9000099090\"\t\"9098-09-90\"\t\"JTNk99Hb9J0090009\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8088\"\t\"9000099009\"\t\"9098-09-99\"\t\"JTNk99HbXJ0090900\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8088\"\t\"9000090999\"\t\"9098-09-99\"\t\"JTNk99Hb0J0090998\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8090\"\t\"9000099999\"\t\"9098-09-99\"\t\"Rbakk9Hr9J0999889\"\t\"09\"\t\"k\"\t\"89,999\"\t\"\"\t\"0,899.90\"\t\"0.00%\"\t\"80,990.90\"\r\n\"8900\"\t\"9000098000\"\t\"9098-09-09\"\t\"JTNk99HbXJ0090809\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000099099\"\t\"9098-09-90\"\t\"kHFk99F08J9099990\"\t\"09\"\t\"k\"\t\"99,900\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"09,090\"\r\n\"8900\"\t\"9000099899\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009808\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000099809\"\t\"9098-09-99\"\t\"JTNk99Hb9J0000000\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000099890\"\t\"9098-09-99\"\t\"JTNk99Hb9J0009990\"\t\"09\"\t\"k\"\t\"99,800\"\t\"\"\t\"9,990\"\t\"0.00%\"\t\"98,990\"\r\n\"8900\"\t\"9000090089\"\t\"9098-09-99\"\t\"JTNk99Hb9J0000899\"\t\"09\"\t\"k\"\t\"89,999\"\t\"\"\t\"9,999.90\"\t\"0.00%\"\t\"89,990.90\"\r\n\"8989\"\t\"9000099009\"\t\"9098-09-99\"\t\"kHFkT9F09c9080909\"\t\"09\"\t\"k\"\t\"08,000\"\t\"\"\t\"9,800\"\t\"0.00%\"\t\"08,800\"";
int TextLengthInBytes = Encoding.UTF8.GetByteCount(MyText); // 5127.
FileStream Nonsense = new FileStream("D:\180415_21569.txt", FileMode.CreateNew, FileAccess.Write, FileShare.None);
StreamWriter Insanity = new StreamWriter(Nonsense, Encoding.UTF8);
Insanity.Write(MyText); // Position 0 => 5123.
Insanity.Flush();
Nonsense.Dispose();
对于此示例字符串,差值 (5123) 比正确值 (5127) 小 4。使用我的原始字符串,它减少了 7 (5120)。
我想知道这种差异背后的原因,以及解决它的可能性。 这似乎与默认缓冲无关,因为结果值有时是奇数而不是偶数,而且它在进行微跳跃。
我正在考虑将上面显示的 GetByteCount
作为跟踪真实位置的替代方法,尽管它与实际感兴趣的 point/code 相距甚远,假设值始终相等,并且很可能是完全临时
希望有人对此有所了解。转发谢谢
编辑:
@Daisy + @Jeroen,刚刚了解 BOM,显然我的原始字符串来自无 BOM 文件,而上面的示例当然是添加 BOM,可爱。另外,我认为我也在冲洗后进行了测试,但似乎我没有...
您是在调用 Flush() 之前还是之后获得位置?我认为这就是问题所在。在 Flush() 之前,不能保证所有内容都已写入。