简体   繁体   中英

space character / streamwriter problem C#

I tried to make a fixed width file, but I found that when I use the space character I have a strange signs in my txt file. If I change the "50" number to something like "15" or "1000", it works fine.

This is my code:

string fileName = Path.GetTempPath() + "temp.txt";
FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream);
writer.Write("1".PadRight(50, ' '));
writer.Write("0".PadRight(756, ' '));
writer.Close();

What is wrong?

Disregard what you read here previously. I took the output of your program, which does shows up strange in Microsoft Notepad, and opened it in a few other editors. It appears fine in all of them. Convinced it was some encoding error, I then cracked open XVI32 to look at the binary.

I removed a single space from the end of the file and I re-save the file and it shows up fine. I put it back in and the problem returned. I change the first hex character from 32 to anything less than 30 and it works fine.

So this seems like a very bizarre error with Microsoft Notepad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM