How to handle special International characters in C#.
-
Hi all, I have written a small piece of code which splits a text file into "N" number of output files with "X" records each. The code is working fine, But it is unable to handle special characters. When the input file has text like --> "INDUSTRIESTRAßE" It's converting it to --> "INDUSTRIESTRA?E". I have not included any provision to handle the special characters. How can I change the code so that it doesn't misinterpret these special characters while splitting a file. This is the code which reads and writes the data.
using (StreamReader sr = new StreamReader(input\_file\_path + "\\\\" + input\_file\_name)) { if (firstLineColumns) header = sr.ReadLine(); //Ignore the first line if the first line is columns while (!sr.EndOfStream) { fs = new FileStream(target\_path + "\\\\" + target\_file\_pattern + file\_no.ToString() + extension, FileMode.Create); sw = new StreamWriter(fs); if (targetFirstLineColumns) sw.WriteLine(header); for (int i = 0; i < number\_of\_lines\_per\_file && !sr.EndOfStream; i++) { sw.WriteLine(sr.ReadLine()); } sw.Close(); fs.Close(); file\_no++; } }
Here I want to split the input to "N" number of files with the "X" number of lines each. Please help me out here. Thank you very much! You don't have to be AFRAID!
-
Hi all, I have written a small piece of code which splits a text file into "N" number of output files with "X" records each. The code is working fine, But it is unable to handle special characters. When the input file has text like --> "INDUSTRIESTRAßE" It's converting it to --> "INDUSTRIESTRA?E". I have not included any provision to handle the special characters. How can I change the code so that it doesn't misinterpret these special characters while splitting a file. This is the code which reads and writes the data.
using (StreamReader sr = new StreamReader(input\_file\_path + "\\\\" + input\_file\_name)) { if (firstLineColumns) header = sr.ReadLine(); //Ignore the first line if the first line is columns while (!sr.EndOfStream) { fs = new FileStream(target\_path + "\\\\" + target\_file\_pattern + file\_no.ToString() + extension, FileMode.Create); sw = new StreamWriter(fs); if (targetFirstLineColumns) sw.WriteLine(header); for (int i = 0; i < number\_of\_lines\_per\_file && !sr.EndOfStream; i++) { sw.WriteLine(sr.ReadLine()); } sw.Close(); fs.Close(); file\_no++; } }
Here I want to split the input to "N" number of files with the "X" number of lines each. Please help me out here. Thank you very much! You don't have to be AFRAID!
Hi, When using StreamReader and StreamWriter you need to set a character Encoding:
... StreamReader sr = new StreamReader(input_file_path + "\\" + input_file_name, Encoding.Unicode) ...
(and the same for the writer). There are other encoding formats aswell. Hope this helps.
Matthew Butler
-
Hi, When using StreamReader and StreamWriter you need to set a character Encoding:
... StreamReader sr = new StreamReader(input_file_path + "\\" + input_file_name, Encoding.Unicode) ...
(and the same for the writer). There are other encoding formats aswell. Hope this helps.
Matthew Butler
Thank you for your information. I'll try that and get back to you.
You don't have to be AFRAID!