0

Whenever I try to convert BinaryReader PeekChar or ReadChar to string it gives me an error

Error 1 'System.IO.BinaryReader.PeekChar()' is a 'method', which is not valid in the given context

How do I convert it? Here is my code sample:

 private void openTextToolStripMenuItem_Click(object sender, EventArgs e)
    {
        myPath = textBox3.Text;
        BinaryReader objBinReader = new BinaryReader(File.Open(myPath, FileMode.Open));
        listBox1.Hide();
        richTextBox1.Show();

        richTextBox1.Text = "";
        do
        {

            try
            {
                richTextBox1.Text = richTextBox1.Text + objBinReader.ReadChar.toString();
            }
            catch
            {
                MessageBox.Show(objBinReader.PeekChar.toString());
            }
        } while (objBinReader.PeekChar.toString() != "-1");

        objBinReader.Close();

    }

Thanks in advance!

4
  • The error is pretty clear - you are trying to use a method as if it were a property Commented Oct 19, 2016 at 7:30
  • 1
    Next compilation error will be that toString() doesn't exist. The method is called ToString() Commented Oct 19, 2016 at 7:31
  • PeekChar returns an int, not a Char. You'll have to cast it to Char before using it, otherwise you'll get a string with the numerica value Commented Oct 19, 2016 at 7:33
  • maby you should fixe some more problems (putting your binary reader in a using block, using a StringBuilder instead of string concatination since string is immutable) Commented Oct 19, 2016 at 8:16

2 Answers 2

2

You are missing the () for the method calls

richTextBox1.Text = richTextBox1.Text + objBinReader.ReadChar().ToString();

and

objBinReader.PeekChar().ToString()
Sign up to request clarification or add additional context in comments.

1 Comment

toString() will be the next compilation error and result in unexpected results - PeekChar returns an int
0

In fact, you're reading the file char after char. Why not do it in one (easy) go?

 private void openTextToolStripMenuItem_Click(object sender, EventArgs e)
 {
     listBox1.Hide();

     richTextBox1.Text = File.ReadAllText(textBox3.Text);

     richTextBox1.Show();
 }

An alternative solution with BinaryReader will be

 private void openTextToolStripMenuItem_Click(object sender, EventArgs e)
 {
     listBox1.Hide();

     // when building string in a loop use StringBuilder
     StringBuilder sb = new StringBuilder();  

     // do not close BinaryReader manually, put using instead 
     using (BinaryReader objBinReader = new BinaryReader(File.OpenRead(textBox3.Text)))
     {
         // PeekChar() is a method, notice () 
         while (objBinReader.PeekChar() != -1) 
             sb.Append(objBinReader.ReadChar()); // ReadChar() is a method as well
     } 

     richTextBox1.Text = sb.ToString();
     richTextBox1.Show();
 }

2 Comments

maby 'caus the file is a binary file? just asking
@Radinator: every file can be treated as text one (it will be a weird text, however, for real binary files like dll, exe); the code in the question uses PeekChar, ReadChar, not PeekByte and ReadByte, so it's a clear attempt (for me, at least) to read the file as text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.