Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading text file that contains special characters

Status
Not open for further replies.

cohoonma

MIS
Sep 2, 2003
18
US
Folks:

I am trying to create a simple C# windows app that contains a couple of file list boxes and a text box. When the entry in the file list box is selected, I want the contents to display in the text box. Simple enough. My problem is that I am trying to display Microsoft SMS MIF files from SMS clients that had an issue trying to insert their MIF file into the SMS database. When I look at the file using notepad, the file contains wierd characters like these:  Õ throughout the file. Note: these characters are supposed to be there by design as SMS creates the files that way.

When I try to dump the file into a text box, all I get is the first wierd character. I am guessing the text reader is interpreting the character as an end of file. Does anyone know how I can just dump the whole file into the text box, regardles off what text it contains? My code is listed below:
Code:
try
{
// Determine if the file exists before loading.
if (System.IO.File.Exists(file))
  {
  // Open the file and use a TextReader to read the contents into the TextBox.
  System.IO.FileInfo myFile = new System.IO.FileInfo(file);
  System.IO.TextReader myData = myFile.OpenText();;

  txtInventoryFile.Text = myData.ReadToEnd().ToString();
  myData.Close();
  }
}
// Exception is thrown by the OpenText method of the FileInfo class.
catch(System.IO.FileNotFoundException)
  {
   MessageBox.Show("The file you specified does not exist.");
  }
// Exception is thrown by the ReadToEnd method of the TextReader class.
  catch(System.IO.IOException)
  {
   MessageBox.Show("There was a problem loading the file into the TextBox. Ensure that the file is a valid text file.");
  }
Thanks,
Michael
 
Temporarily give the file a non text file extension eg .bin and open it in VS.

You should see a hex dump of the file and from that sdhould be able to identify the offending characters.


Hope this helps.

[vampire][bat]
 
earthandfire,

thanks, however, I really don't care what the characters are. I am just scanning the file to find a computer name that is contained within it. I use that infor for other items related to SMS Administration. Just a complete dump of the file is all that I am looking to do regardless of whether the file has any wierd characters or not. Do you think that is possible?

Michael
 
Sorry, I didn't read the last sentence of you original post properly.

[vampire][bat]
 
If you know the encoding for the file, you can read the file as a series of bytes, strip out the offending bytes, and then call one of the Encoding family of classes (ASCIIEncoding, Utf8Encoding, EBCDICEncoding, etc) to convert them to a human-readable string with the GetString() method.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip,

Not being a full-time programmer, I tried researching your idea of encoding.

I grabbed a couple of examples from MSDN and tried reading in the file and seeing what happens.

I unfortunately got the same results. My guess is I need to somehow figure out what encoding, if any, SMS is using to write the inventory file I am trying to dump into the text box. Is there someway to determine the encoding?

Michael
 
You can make some guesses, but generally unless they tell you, it's difficult to determine. Given that it's a Windows program creating it, my guesses in decreasing order of probability are:

1. Win-1251 (Latin-1)
2. Whatever the default locale for the operating system is.
3. UTF-16
4. UTF-8

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Well, I have come up with a solution.
I took the approach of not trying to dump the whole file in to the textbox. I instead stumbled upon using the IndexOf() function to find the start of the computer name, then the Substring() function for the correct length. I guess I thought it would have been easier to just dump the whole file rather than pick out the computer name. Turned out to be the exact opposite though. the index and substring made it easy.

thanks for all of your time and suggestions.
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top