How to overwrite file data regularly?

Hey guys,

so I have this logging script that I modified so that it would only provide me with the x und z position of a moving game object I want to follow (I want to transform x and z into latitude/longitude later on - but that’s another story).

So what I am trying to do is create file that Unity opens up after a set time, then it overwrites the old “coordinates” and closes the file again. So basically the file should hold two constantly changing parameters which can also be read from another application in between the changes Unity makes.

So this is what I came up with/modified from an existing log file:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System;
using System.Collections.Generic;

public class DisplayLocation : MonoBehaviour 
{
	public TextWriter fs;
	public GameObject playerobject;
	public Vector3 playerlocation;
	public bool PositionLogging = true;
	float deltaTime = 10f; // every 10s
	float timeToLog;

	void Start () 
	{
	playerobject = GameObject.FindGameObjectWithTag("toFollow");
		playerlocation = playerobject.transform.position;

		if (!Directory.Exists (@".\positionUpdate"))
			Directory.CreateDirectory (@".\positionUpdate");

		var nameOfLogFile = ("currentPosition"+"")+ ".csv";
		
		this.fs = new StreamWriter(Path.Combine("./positionUpdate",nameOfLogFile), true){AutoFlush = true};
	}

	void Update() 
	{
		if (Time.time < timeToLog || !PositionLogging) return;
		timeToLog = Time.time + deltaTime;

		fs.Write(playerobject.transform.position.x);
		fs.Write(Environment.NewLine);
		fs.Write(playerobject.transform.position.z);
		fs.Close ();
	}
}

The problem I have is, that the StreamWriter seems to always append text instead of overwriting it. Also as soon as I close the writer to free up the file for reading, I get an error (the object was used after being disposed).

Do you guys have any ideas where and how I went wrong and how to fix this problem?

Thanks already!

Lukas

I think I found a way:
Instead of just “running” the StreamWriter, I used the “using” statement so that the Stream is automatically disposed of after being used.

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System;
using System.Collections.Generic;

public class Locationtry : MonoBehaviour 
{
	//public TextWriter fs;
	public GameObject playerobject;
	public Vector3 playerlocation;
	public bool PositionLogging = true;
	float deltaTime = 10f; // every 10s
	float timeToLog;
	
	void Start () 
	{
		playerobject = GameObject.FindGameObjectWithTag("toFollow");
		
		if (!Directory.Exists (@".\positionUpdate"))
			Directory.CreateDirectory (@".\positionUpdate");

		using (var createFile = File.Create(@".\positionUpdate\currentPosition.csv.")) 
		{
		}
	}
	
	void Update() 
	{
		if (Time.time < timeToLog || !PositionLogging) return;
		timeToLog = Time.time + deltaTime;


		using (FileStream fs = new FileStream(@".\positionUpdate\currentPosition.csv.", FileMode.Open, FileAccess.Write))
		using (StreamWriter sw = new StreamWriter(fs))
		{
			sw.WriteLine(playerobject.transform.position.x);
			sw.WriteLine(playerobject.transform.position.z);
		}

	}