Matthew Young Matthew Young - 1 year ago 85
C# Question

StreamWriter not writing all characters

I have this bit of code run when people close the game to save all their currently selected pets (this is for school don't worry about how I named it "Squirtle", no copyright problems here). The code in question is:

using (StreamWriter sw = File.CreateText(@"..\..\pets.txt"))
sw.AutoFlush = true;
foreach (Pet p in petList)
sw.Write(p.PetID + ' ');
sw.Write(p.Name + ' ');
sw.Write(p.HP + ' ');
sw.Write(p.Type + ' ');
sw.Write(p.Level + ' ' );
sw.Write(p.Rarity + ' ');

the commas were spaces and I just added the autoflush to try and fix the problem, but basically no matter how many times I run it, the first two, next two, and last 3 pieces of data have no spaces between them, example: 32SQUIRTLE 77AQUATIC 41930. this happens every time I run it and am wondering if anyone knows of why it's doing this? I can use any delimiter also if space and comma are notorious with StreamWriter or something.

Answer Source

You are doing Int + Char, The overload for this operation would results in Int + (int) Char which is casting the space into its Unicode code point and doing arithmetic operation

What you want to do is p.PetID.ToString() + " "

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download