writing a string to a file

Hi

I’m trying to write some game object data into a text file. I have tried a few different ways of doing this but everytime I end up with a empty text file. the string I’m trying to write to the textfile is constructed like so…

string constructString = type + "," + relativeXPosition + "," + relativeYPosition + "," + relativeZPosition + "," + relativeXRotation + "," + relativeYRotation + "," + relativeZRotation;

the latest method I have used to try and write the data to the file is this…

using(StreamWriter sw = new StreamWriter(savename))
		{
			sw.WriteLine(constructString);
			sw.Flush();
			sw.Close();
		}

am I missing something or doing something terribly wrong?

using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate))
{
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, objToSerialize); //write
    formatter.Deserialize(stream); //read
}

//filepath is something like this: Path.Combine(Application.dataPath, "filename.xxx")

and when you have using it means you don’t have to close that stream, it is autoclosed

Try using append:

using(StreamWriter sw = new StreamWriter(savename, true))

alright, lets see

what namespace does BinaryFormatter use?
and is objToSerialize supposed to be the complete GameObject?
I’d rather just save the string like I have it constructed there.

using(StreamWriter sw = new StreamWriter(savename, true))

that still doesn’t work, but sure I want to use append, no use in replacing my string as a bunch of objects will be called to construct their string and write it into the .txt.

The save path is constructed like so…

string pathToSave = filePath + "/" + saveName + ".txt";
				
				if (!Directory.Exists (filePath)) {
					Directory.CreateDirectory (filePath);
				}
				File.Create(pathToSave);

where filePath is Application.persistentDataPath. is it possible that im creating the file in one place and then trying to write to another location? That doesnt seem to be happening but i could be missing something somewhere. i’m also not getting any errors popping up so maybe my logic is just flawed somewhere.

Thanks guys :slight_smile:

any insight?