0

hello guys))
Advice please - how to set the stream encoding? (to read a text file)
I use this code -

StreamReader stream1 = new StreamReader(fs);
            System.Text.Encoding code = stream1.CurrentEncoding;
            StreamReader stream2 = new StreamReader(fs,code);
            while ((i = stream2.ReadLine()) != null) 
            {
               
                label1.Text = label1.Text + i;
            }
            stream2.Close();

but in label1.text I have only numbers and weird characters ((
tell me - where's the mistake?
Thank you in advance)

3
Contributors
16
Replies
17
Views
6 Years
Discussion Span
Last Post by vedro-compota
Featured Replies
  • Why do you want to encode a text? A text its alredy encoded, dont you think. You only need to get it from the file into a label. Read More

  • Where it hangs up? Or is it tryig to go into infinity in the while loop? If so, that means its still reading the file - mayne you read a file byte by byte (like I showed you on the previous example) and this is taking very long time - … Read More

  • I have chnaged a code a bit, and now it works: [CODE] private void ReadingFile() { using (StreamReader sr = new StreamReader(@"C:\1\map.txt")) { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { if … Read More

  • 1
    __avd 1,826   6 Years Ago

    To display multi-lingual chars set Unicode font. [code] string[] lines = System.IO.File.ReadAllLines(@"c:\file.txt", System.Text.Encoding.Unicode ); label1.Font=new Font("Arial Unicode MS",10f); foreach (string l in lines) label1.Text += l + "\n"; [/code] Read More

1

Why do you want to encode a text? A text its alredy encoded, dont you think. You only need to get it from the file into a label.

Comments
+++++
0

You can do it like that:

using (StreamReader reader = new StreamReader(@"C:\myFile.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                label1.Text += line + Environment.NewLine;
            }
        }
0

if i run this -

StreamReader reader = new StreamReader(@"C:\map.txt");
        
            string line;
            while ((line = reader.ReadLine())  != null)
            {
                label1.Text += line + Environment.NewLine;
            }

in label1.text I have only -

������� 20 20 2 4 5
������ 40 50 1 3 4 6 7
������ 70 90 3 6 9
������ 23 90 1 3 4 8 9
����� 34 100 7 8
�����-���� 300 300
�������� 67 83 2 3 8
����� 92 63 1 4 5 6
����������� 400 300 2
��������� 500 400 2

0

The problem is that words are not written in English))) tell me - how to read a file character by character?

0

Not in english? So this is useless then.
But there is the code you wanted - to read character by character:

using (StreamReader sr = new StreamReader(path)) 
            {
                //This is an arbitrary size for this example.
                char[] c = null;

                while (sr.Peek() >= 0) 
                {
                    c = new char[1]; //1 character at a time
                    sr.Read(c, 0, c.Length);
                    //The output will look odd, because
                    //only five characters are read at a time.
                    Console.WriteLine(c);
                }
            }

Hope it does some help to you.

0

but can I do something like this -

StreamReader reader = new StreamReader(@"C:\map.txt");
        
            string line;
            while ((line =[B] reader.Read().ToString())[/B]  != null)
            {
                label1.Text += line + Environment.NewLine;
            }

?
why in this case, the program hangs?

Edited by vedro-compota: n/a

1

Where it hangs up? Or is it tryig to go into infinity in the while loop? If so, that means its still reading the file - mayne you read a file byte by byte (like I showed you on the previous example) and this is taking very long time - depends of the file lenght.
Otherwise I cannot see any other reason to hang up.
Take a look in a debug mode (put the break point and go line by line with pressing F11).

Hope it helps,
Mitja

0

Where it hangs up?

thank you very much , Mitja Bonca !
I forgot to change the loop condition)) - now everything works)) but wrong)) -

StreamReader reader = new StreamReader(@"C:\map.txt");
        
            string line;
            int i=0;
            while ((i= reader.Read())  != -1)
            {
                line = i.ToString();
                label1.Text += line ;
                textBox1.Text += line ;
            }

there's only one line in the file -

Voronezh 20 20   2 4 5

but in label1.text I have -

861111141111101011221043250483250483232325032523253131013101310

Edited by vedro-compota: n/a

0

Would you mind giving me the content of the file? So I can try it on my own.

btw, where you read: Voronezh 20 20 2 4 5

1

I have chnaged a code a bit, and now it works:

private void ReadingFile()
        {
            using (StreamReader sr = new StreamReader(@"C:\1\map.txt"))
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    if (line != String.Empty)
                        label1.Text = line + Environment.NewLine;
                }
            }
        }

I hope it does work for you too.
Mitja

1

To display multi-lingual chars set Unicode font.

string[] lines = System.IO.File.ReadAllLines(@"c:\file.txt", System.Text.Encoding.Unicode );

label1.Font=new Font("Arial Unicode MS",10f);
foreach (string l in lines)
    label1.Text += l + "\n";
Comments
++++++++
0

there is an easier way to work with a certain (multilingual, inter alia) encoding and character by character reading. here it is.

string FilePath = Application.StartupPath + @"/map.txt";
            StreamReader reader = new StreamReader(FilePath,Encoding[B][U].UTF8[/U][/B]);
            [B]char[] c[/B] = new char[1];
            int i=0;
            while ((i= reader.Read())  != -1)
            {
                c[0]=  Convert.ToChar(i);
                [B]string line = new string ( c);[/B]
                label1.Text += line ;
                textBox1.Text += line ;
            }

If anyone knows how we can improve this algorithm - share knowledge))

0

What are you trying to get with using Read() method?
This method returns an integer (not a string) so that it can return -1 if the end of the stream has been reached (like you did in your example), but I still dont get what are you trying to get - if you want to get the string inside the file, it will simply not go.
This why you have to use ReadLine(), or method like adapost has shown you in post above.

I hope this helps clearing the issue here.
best regards,
Mitja

1

I just wanted to read a file character by character - and, each time getting a string value of integer code (in accordance with the encoding))) thank you very much for your help))

This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.