MTSkull Posted March 2, 2007 Posted March 2, 2007 I am creating an application that will interface with an old service. I need to put a single line code into a text file to get it to do stuff elsewhere on the network. One of the requirements is the code must be a single line with no new line or whitespace beginning or end. Here is the code I am attempting to use. When I write the single line to the file and then read the file it shows as 4boxes (unrecognized char code?). Thanks MTS //c# public Int32 IssueSerialNumber() { //Writes to a file monitored by a server service. When the service detects the appropriate code Int32 PassFail = LocalError.ERROR_GEN_PASS; StreamReader LFRead = new StreamReader("C:\\Lock001.txt"); string sTemp = LFRead.ReadLine(); if (sTemp != "RU") { //TODO the file is not ready, handle it //return LocalError.ERROR_FILE_Something_New_Happened; } LFRead.Close(); StreamWriter LFWrite = new StreamWriter("C:\\Lock001.txt"); LFWrite.Write("S001_D1T"); LFWrite.Close(); return PassFail; } Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
Administrators PlausiblyDamp Posted March 2, 2007 Administrators Posted March 2, 2007 Does the file need to be in any particular characterset or encoding? Try something like StreamWriter LFWrite = new StreamWriter("C:\\Lock001.txt", false, System.Text.ASCIIEncoding.ASCII); to create the StreamWriter as the default is UTF-8 which could well be confusing the old application. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MTSkull Posted March 2, 2007 Author Posted March 2, 2007 No Change I did however do this which is almost right LFWrite.Write("S001_D1C\x00", false, System.Text.ASCIIEncoding.ASCII); Forcing the null to the end of the string to get it to see this as a string. This produces the file text "S001_D1C " Then if I could just get it to drop the white space at the end I would be alright. Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
MTSkull Posted March 5, 2007 Author Posted March 5, 2007 Here's how I solved it. I thought maybe it was not recognizing it as a string, so I terminated the characters with a "\x00" or an ASCii null char 0. This worked but it placed white space at the end. Next I tried sTemp = "S00" + Station_Num.ToString() + "_" + BootVariables.Model_Code;// +"\x00"; LFWrite.Write(sTemp, false, System.Text.ASCIIEncoding.ASCII); so I could check the string contents in debug before actually sending it out. Then I tried dropping the null char to see what would happen and it worked correctly. I think this works because the string variable handled formatting it as a string where pushing the raw text directly into the write function did not. Or something like that. MTS Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.