Search code examples
c#filestreamstreamwriter

Getting Exact Stream Position After Writing


Checking for FileStream.Position after using StreamWriter.Write(), I'm facing some inaccuracy in the given value, which I verified against the lengths of both produced file and used string:

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();

With this sample string, the difference is 4 less (5123) than the correct value (5127). With my original string it's 7 less (5120).

I'd like to know the reason behind this difference, and possibilities to resolve it. It doesn't seem like it's about default buffering, since the resulting value is sometimes odd, not even, and it's doing micro-jumps.

I'm considering GetByteCount shown above as an alternative to tracking true position, although it's distancing from the actual point/code of interest, assumes the values are always equivalent, and might very well be fully ad hoc.

Hope someone has a clue on this. Forward thanks.

EDIT:

@Daisy + @Jeroen, just learning about BOM, and apparently my original string came from a BOM-less file, while the sample above is of course adding BOM, lovely. Also, I thought I tested after flushing as well, but seems I didn't...


Solution

  • Are you getting the position before, or after the call to Flush() ? I think that's where the problem is. Before Flush() it's not guaranteed that everything has been written.