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

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.

Related

How read form file Text language Arabic

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

How to enter multiple data from textbox to txtfile without overwrite [duplicate]

This question already has answers here:
Append lines to a file using a StreamWriter
(11 answers)
Closed 7 years ago.
i need some help with entering data in a txt.file.
this is the following code:
StreamWriter file = new StreamWriter("opslag_kentekens");
string opslag_kentekens = textBox1.Text;
file.WriteLine(opslag_kentekens);
file.Close();
label20.Text = File.ReadAllText("opslag_kentekens");
So when i click on my button the text what is entered in the textBox1.text
has to go to my opslag_kentekens.txt. this works fine but when want to enter new text to my txt, it overwrites the first entered text. I want every text whats entered among each other. How do i do this? (sorry for my bad english).
file.WriteLine() will not keep your existing text.
You can use File.AppendAllText(String, String) instead:
https://msdn.microsoft.com/en-us/library/ms143356(v=vs.110).aspx
try this
new StreamWriter("opslag_kentekens", true);
Change your constructor to use the append overload and set it to true, that should work.
StreamWriter file = new StreamWriter("opslag_kentekens", true);
Basically you're looking at appending to a file:
From msdn:
public static void Main()
{
string path = #"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
Usually, for writing (not appending), it's easier to use the File Write methods, as they are cleaner and convey your meaning better:
var some_text = "this is some text";
var out_path = #"C:\out_example.txt";
System.IO.File.WriteAllLines(out_path, some_text);
Even better and cleaner, look #Liem's answer, which is the same but with the correct Append syntax.

Write to a txt file right to left

I want to write a message in Hebrew to a text file. I tried the following:
using (StreamWriter sw = File.CreateText(path + FileName))
{
sw.WriteLine(sms);
}
However, the message comes out left to right. How can I write right to left?
A string is an Array of Chars so you can
Char[] tempArray = sms.ToCharArray()
Array.Reverse(tempArray)
sms = new String(tempArray)
This should reverse the array therefore when you write it will read right to left.
well, it doesn't really matter how you write them in.
Here's a little example:
void Main()
{
string lines = "First line.\r\nSecond line.\r\nThird line.";
var = new System.IO.StreamWriter("c:\\test_eng.txt");
file.WriteLine(lines);
file.Close();
string hebrew = #"מספרים רצים מימין לשמאל ?";
file = new System.IO.StreamWriter("c:\\test_heb.txt");
file.WriteLine(hebrew);
file.Close();
}
Upon opening the files, you'll find that hebrew is hebrew, and display from right to left.
Are you experiencing any funny behavior?
You can also try defining your string culture to hebrew:
// Creates and initializes the CultureInfo which uses the international sort.
CultureInfo myCIintl = new CultureInfo( "he", false );
More on that here (MSDN).
Some more thinking -> I doubt the above will change much for the display, it'll be more useful for dates for example. But atm, I'm not able to see hebrew on my console output ... What are you outputting to? Just txt, HTML, or something else?

Opening a file with french characters using StreamReader showing wrong incorrect data

I have a file with the following text in: SignOut,déconnectez.
When I use the following code:
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(FileName, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
list.Add(line); // Add to list.
}
I get this back: "Sign Out,d�connectez,"
I thought that opening the file with Encoding.UTF8 would be enough but it doesn't seem to do anything. Could someone point me in the right direction to open a file that may contain non standard characters please?
Use
Encoding.GetEncoding("iso-8859-1");

Load Config file into Windows Forms

I am attempting to build a tool using C# to modify the config files for our software. The config files are in the following format:
SERVER_NAME TestServer
SERVER_IP 127.0.0.1
SERVICE_NUMBER 4
SERVICE_ID 1 2 3 4
And so on. Each line is prefaced with an identifier(Ex: SERVER_NAME), then the value. I need the tool to load the value for each identifier into a separate text box. When the user clicks save, it needs to write the updated information to the file.
I am entirely lost on how I should load the data into the text boxes, so if you could provide some help on that, I would appreciate it.
Writing it, I am assuming the easiest way, since all of the data will be loaded, is to erase the previous data, and write the new data to the file. This I should be able to handle without a problem. If there is a better way to do this, I am definitely willing to try.
I would greatly appreciate some pointers on how to get started with loading the data.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFD.ShowDialog();
openFD.Title = "Open a Config File...";
openFD.InitialDirectory = "C:";
openFD.FileName = "";
openFD.Filter = "CONFIG|*.cfg";
string selected_file = "";
selected_file = openFD.FileName;
using (StreamReader sr = new StreamReader(selected_file))
{
string currLine;
while ((currLine = sr.ReadLine()) != null)
{
}
}
}
To read each line of the file you could do something like this:
// StreamReader is in System.IO
using(StreamReader sr = new StreamReader("config file path here"))
{
string currLine;
while((currLine = sr.ReadLine()) != null)
{
// currLine will have the current line value as a string
// You can then manipulate it any way you like
// Or store it in an array or List<>
}
}
If you need help with adding items into a textbox, just ask.
Hope this helps!

Categories