Victor BA

I have an app that opens a text file, but i can't see the accents, instead i see something like this:

cu l es l

the problem is by reading the file or a property in the textbox where is displayed



Re: Visual C# General Text file symbols

Peter Ritchie

Open the text file with what application, yours

If it's just your application with the problem, how are you reading/writing the data in the text file






Re: Visual C# General Text file symbols

Victor BA

Yes the problems happens in my app, because in Notepad works perfectly.

This is the fragment of code that i use:

FileStream fs = File.OpenRead(txtArchivo.Text);

StreamReader sr = new StreamReader(fs);

textBox1.Text = sr.ReadToEnd();

i've seen similar code in books but they didn't mention this could cause problems Tongue Tied





Re: Visual C# General Text file symbols

Peter Ritchie

Using StreamReader in that way attempts to decide what encoding is used in the file based upon byte order marks. If no byte order marks are present then it defaults to UTF8. If the text in your file is not stored with UTF8 encoding it will not know how to decode all possible characters in the file (or will decode them incorrectly). If you know what encoding is used for the file, you should specify that by using the StreamReader(Stream, Encoding) constructor.




Re: Visual C# General Text file symbols

Victor BA

I selected the Encoding.UTF7 and worked, but i was thinking, how would i know which encoding is used on each file that i try to open

is there a way to detect this , so then you select the appropiated encoding method





Re: Visual C# General Text file symbols

Peter Ritchie

That's why byte-order-marks were invented. You can't tell what encoding any other application is using without byte-order-marks. The only thing you can do is assume and document those assumptions for other application programms, if applicable.




Re: Visual C# General Text file symbols

Victor BA

I checked what you said about BOM (byte-order-marks) and used the constructor as following:

StreamReader(fs,true);

so it can search for the encoding, but that didn't work, then i tried this another form:

StreamReader(fs,Encoding.Default,true)

and that works, but only because the Encoding is set to default, still works if i remove the "true" setting, so is not affecting.

i'm i using the correct way to set BOM or what i'm i doing wrong





Re: Visual C# General Text file symbols

Peter Ritchie

StreamReader(fs,true) is effectively the same as StreamReader(fs, Encoding.UTF8, true).

When use use Encoding.Default it will use whatever codepage Window has loaded for your system to decide what Encoding to use.