Single Line File Write

MTSkull

Well-known member
Joined
Mar 25, 2003
Messages
135
Location
Boulder, Colorado
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
Code:
//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;
        }
 
Does the file need to be in any particular characterset or encoding?
Try something like
C#:
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.
 
No Change

I did however do this which is almost right

Code:
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.
 
Heres 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
Code:
            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
 
Back
Top