Thursday, July 30, 2009

I am getting errors using the Peek() function in C# for a file.?

It says that the Input string was not in a correct format. I made a text file with an int followed by a String. I wanted to test out the Peek() function so I first read in the int value and then called the Peek() expecting the string to return and I got this error. What do i do to fix it?

I am getting errors using the Peek() function in C# for a file.?
Hello,





Peek returns an integer which corresponds to one byte read in the file: example if you read an "A" you will get the value 64.





Below is an example of using peek() to read the first byte of a file and convert it back to a printable character.





using System;


using System.IO;


using System.Text;





namespace ConsoleApplication0


{


class Program


{


static void Main(string[] args)


{


using (StreamReader sr = File.OpenText("test.txt"))


{


int i;


i=sr.Peek();


Console.WriteLine("First char in file is: {0}",Convert.ToChar(i));


sr.Close();


} } } }


No comments:

Post a Comment