Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have sample.text file ( UTF8 encoded format) having maximum size of 200 character. I have 2 line of data(word count=38 and total byte count=40) present in sample.text file as given below:
---------------------------------------------
019aIŞLET NO : 044
019aIŞLET NO : 045
---------------------------------------------
Here i need to write new data (assume new data is string newData="Hello C#.NET" at the end of file. here is snippet code for that:
------------------------------------------------
C#
var file = new FileInfo(sample.text);
var contentUTF8 = File.ReadAllText(file.FullName, Encoding.UTF8);
FileStream fileStream = new FileStream(sample.text, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 8192, FileOptions.WriteThrough);
fileStream.SetLength(200); // setting max length of file and assign null to remaining data length(remaining 160 characters)
fileStream.Position = Encoding.UTF8.GetByteCount(contentUTF8); // setting new position to write newData, which should be 40 so it will start writing from 41st position. 
StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);
streamWriter.Write(newData);
streamWriter.Flush();

------------------------------------------------
Since total byte length present sample.text is 40, above sample code should write newData from 41st position BUT while writing newData to sample.text file it overrides last 3 character of second line of sample.text file.
New sample.text file becomes as given below:
-------------------------------------
019aIŞLET NO : 044
019aIŞLET NO : Hello C#.NET
-------------------------------------
But sample.text should looks(in expected case) like:
-------------------------------------
019aIŞLET NO : 044
019aIŞLET NO : 045Hello C#.NET
-------------------------------------

Conclusion of issue is this irrespective of input file length when i am writing newData to my file it always overrides last 3 characters. This is happening only in case of UTF8 encoding.
In case of ANSI encoded file it works fine without any issue.
Can anybody tell me why my sample code(streamWriter.Write(newData)) is overriding last 3 characters of my existing UTF8 encoded file?
I hope i have tried to explain problem properly, please let me know in case more information is required.
Posted
Comments
DamithSL 2-Jun-15 3:40am    
try File.AppendAllText("sample.text", newData);
Vibhukant Singh 2-Jun-15 3:46am    
Thanks a lot for your suggestion But
i am not expecting any alternative solution. i need to understand why its overriding last 3 characters(last 3 positions) when i am using the above solution/code in case of UTF8 encoded file.
Richard MacCutchan 2-Jun-15 4:25am    
Use your debugger to check what is being read in and what value is returned by Encoding.UTF8.GetByteCount(contentUTF8);.
Vibhukant Singh 2-Jun-15 4:49am    
Its returning value 40 means length of byte used to store content sample.txt file.
Actual character count is 38 only. But since its UTF8 encoded file, there is 2 Unicode character present in sample file and each stores 2 byte of data, hence total byte count is 40.
Any problem you are facing ?
or
Any observation ?

1 solution

The problem is preamble. Preamble is a couple of bytes at the start of file which determines the encoding. As it happens it's exactly 3 bytes long for UTF-8. So when you set the position you should take that into account:
C#
fileStream.Position = Encoding.UTF8.GetByteCount(contentUTF8) + Encoding.UTF8.GetPreamble().Length;
 
Share this answer
 
Comments
Vibhukant Singh 2-Jun-15 4:53am    
Thanks a lot Tomas. It worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900