How read form file Text language Arabic - c#

I have a text file that contains a single word but it is with language : Arabic
I want to extract it
My code is:
string text = System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt");
Console.WriteLine(text );
I have the result with unknown characters : ????
How i can fix it?
Thanks,

Setup right codepage for your text.
System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt",System.Text.Encoding.GetEncoding(codepage))
May be codepage=1256 (windows-arabic).

Your code reads the text correctly into the variable text. (Debug and See)
However, dispalying arabic characters in the windows Console is another issue (Check how to solve it Here)

You can try this:
string text = System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt",Encoding.Default);
Console.WriteLine(text);

Try specifying the encoding using this StreamReader constructor:
StreamReader arabic_reader = new StreamReader(filePath, System.Text.Encoding.UTF8, true);
OR
string text = System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt",Encoding.UTF8);

Try :
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, true);
For reference: http://msdn.microsoft.com/en-us/library/ms143457.aspx

Related

Using Character Encoding with streamreader

My program connects to an ftp server and list all the file that are in the specific folder c:\ClientFiles... The issue I'm having is that the files name have some funny character like – i.e. Billing–File.csv, but code removes replace these characters with a dash "-". When I try downloading the files its not found.
I've tried all the encoding types that are in the class Encoding but not is able to accommodate these character.
Please see my code listing the files.
UriBuilder ub;
if (rootnode.Path != String.Empty) ub = new UriBuilder("ftp", rootnode.Server, rootnode.Port, rootnode.Path);
else ub = new UriBuilder("ftp", rootnode.Server, rootnode.Port);
String uristring = ub.Uri.OriginalString;
req = (FtpWebRequest)FtpWebRequest.Create(ub.Uri);
req.Credentials = ftpcred;
req.UsePassive = pasv;
req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
try
{
rsp = (FtpWebResponse)req.GetResponse();
StreamReader rsprdr = new StreamReader(rsp.GetResponseStream(), Encoding.UTF8); //this is where the problem is.
Your help or advise will be highly appreciated
Not every encoding has a class in the encoding namespace. You can get a list of all encodings know in your system by using:
Encoding.GetEncodings()
(MSDN info for GetEncodings).
If you know what the name of the file should be, you can iterate through the list and see what encodings result in the correct filename.
Try:
StreamReader rsprdr = new StreamReader(rsp.GetResponseStream(), Encoding.GetEncodings(1251)) ;
You may also try "iso-8859-1" instead of 1251

When reading a file I get ??? as input instead of Cyrillic letters

Thank you very much for all the help till now.
I have the following problem:
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
using (StreamReader reader = new StreamReader(#"C:\test.csv"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(',');
dict.Add(parts[0],parts[1]);
}
}
Console.WriteLine("enter name:");
string name = Console.ReadLine();
if (dict.ContainsKey(name))
{
//
string value = dict[name];
Console.WriteLine(dict[name]);//value);
}
Console.ReadLine();
}
}
And this is my Outout:
enter name:
ILIA
????
This is my CSV file content
ILIA,ИЛИЯ
Can you please help me out clear this ???? and get the correct symbols in Cyrillic?
Thank in advance for any help!
Ilia
Try :
StreamReader reader = new streamReader(#"C:\test.csv",Encoding.GetEncoding(1251)) ;
I noticed this unpleasant issue while reading files with Cyrillics some time ago. The easiest solution - open the file in Notepad++ or any similar text editor that can change encoding and choose from the menu Encoding -> Convert to UTF-8 for example. Then your file will be processed correctly with your code
The output encoding of your console is likely set to ASCII. Try adding the following command before writing to the console:
Console.OutputEncoding = System.Text.Encoding.Unicode;
It's also possible that the the font that you chose for the console does not support Unicode. To fix this, open the the command console, click on the icon at the top left corner of the window that says "C:\". Select properties. Select Font. Try selecting a different font.

How to display message with special encoding

I am writing a program which I need to read text from a file and display this on the graph once mouse hover the datapoint. My problem is while I read the data from text file and show it on graph it shows some "?" instead of actual character. (cannot post images sorry)
here is my code to read from file and attempted to change encoding.(no success) :
string myString = File.ReadAllText(#"read.txt");
Encoding enc_to = Encoding.GetEncoding("iso-8859-1");
Encoding enc_from = Encoding.UTF8;
byte[] InitialBytes =enc_from.GetBytes(myString);
byte[] FinalBytes = Encoding.Convert(enc_from, enc_to, InitialBytes);
string myMessage = enc_to.GetString(FinalBytes);
Please note that I dont want to show string as MessageBox.Show rather I want to show it as tooltip.
here is the text in read.txt file :
3 stands of 5½"
here is the how it is shown :
3 stands of 5�"
Use Encoding.Default:
string myString = File.ReadAllText(#"read.txt",Encoding.Default);

With StreamWriter doesn't work \n (C#)

I have a problem with the C# Stream Writer.
I use the following Code:
//Constructor
public EditorTXTFile
{
FileStream f = File.Create(System.IO.Directory.GetCurrentDirectory() + "\\Output.txt");
f.Close();
}
//Function AddText
public void AddLogFileText(string text)
{
string text = "l1\n\rl2\n\rl3\n\nl5";
StreamWriter writer = new StreamWriter(System.IO.Directory.GetCurrentDirectory() + "\\Output.txt", true);
writer.Write(text);
writer.Close();
}
When I open Output.txt it shows for \n or \r a █(which means not showable symbol) and the whole string is in one line...
Later should the text hand over the function, so I can't write the text with .WriteLine because I don't know if the actual string is on the same line or in a new line.
What make I wrong?
Thanks for any help.
Use Environment.NewLine as line separator or "\r\n" if you want to do it by hand.
Line Separator(newLine) is \r\n not \n\r,
change your text as :
string text = "l1\r\nl2\r\nl3\r\nl5";
Try string text = #"l1\n\rl2\n\rl3\n\nl5";. To prevent character stuffing.
This is binary format:
writer.Write(text);
This is line sequential format:
writer.WriteLine(text);
You have to use WriteLine format...
You can use Environment.NewLine like this:
streamWriter.Write(String.Concat(Enumerable.Repeat(Environment.NewLine, n).ToArray()));
i tried to write a class and seprate "\n"s
but i found rich text box!!
yeah! it works:
RichTextBox rch = new RichTextBox();
rch.Text = cmn;
foreach (string l in rch.Lines)
strw.WriteLine(l);

how do I feed an rtf string to a richtextbox control

I have a string of richtext characters/tokens that I would like to feed to a richtextbox in code.
string rt = #" {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}"+
#"{\colortbl ;\red255\green0\blue0;}"+
#"{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\par"+
#"\cf1\f1 hello\cf0\f0 \ul world\par}";
I have attempted this :
System.IO.MemoryStream strm = new System.IO.MemoryStream();
byte[] b = Encoding.ASCII.GetBytes(rt);
strm.BeginRead(b, 0, b.Length, null, null);
richTextBox1.LoadFile(strm, RichTextBoxStreamType.RichText);
it didn't work.
can anyone give me a few sugestions.
BTW the rich text comes from saving from wordpad, opening the file with notepad and using the text with in to build my string
Rich textbox has a property named Rtf. Set that property to your string value. Also, your string has an extra space as the first character. I had to remove that before I saw your Hello World.
Expanding on gbogumil's answer:
string rt = #"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}"+
#"{\colortbl ;\red255\green0\blue0;}"+
#"{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\par"+
#"\cf1\f1 hello\cf0\f0 \ul world\par}";
this.richTextBox1.Rtf = rt;

Categories