Matthew Young Matthew Young - 5 months ago 43
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.


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() + " "