i need to read contents of a file(of any format) into a char array, how can i do this

i need to read contents of a file(of any format) into a char array, how can i do this

FileInfo fz = new FileInfo(tempFile);
FileStream fzs = fz.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] data = new byte[fz.Length];
fzs.Position = 0;
fzs.Read(data, 0, Convert.ToInt32(fz.Length));
string strData = Encoding.UTF8.GetString(data);
char[] ch = strData.ToCharArray();

Remember to dispose, and try..catch, all that stuff.
Basically you stream the file, read the stream into a byte array, then encode the byte array into a string where you can turn it into a char array. You may need to try differenent encoding depending on culture settings, etc.

Jerry


Just thought of a one liner that also does this: (if it is a text file you are working with)

char[] ch = File.ReadAllText("filename.txt").ToCharArray();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.