C
C#4mo ago
Mazranel

StreamWriter writing some lines multiple times

I'm using StreamWriter to write project data into a file, but more often than not I'll get some lines written twice and maybe even three times. Is this a bug with my code or something else? Code:
// Write the project data into the file
using (StreamWriter PFWriter = new StreamWriter(ProjectConfigFilePath, false))
{
PFWriter.WriteLine("[PROJECT_INFO]");
PFWriter.WriteLine(ProjectName);

if (OpenScene.Contains(Path.GetDirectoryName(ProjectConfigFilePath)))
{
PFWriter.WriteLine($"INSIDE_PROJECT_DIR -> {OpenScene.Replace(Path.GetDirectoryName(ProjectConfigFilePath), "")}");
}
else
{
PFWriter.WriteLine(OpenScene);
}

PFWriter.WriteLine("[END_PROJECT_INFO]");
PFWriter.WriteLine("\n[SCENES]");

ConsoleUtils.StatusWrite(string.Join(", ", SceneList));

foreach (string Scene in SceneList)
{

if (Scene.Contains(Path.GetDirectoryName(ProjectConfigFilePath)))
{
PFWriter.WriteLine($"INSIDE_PROJECT_DIR -> {Scene.Replace(Path.GetDirectoryName(ProjectConfigFilePath), "")}");
}
else
{
PFWriter.WriteLine(Scene);
}
}

PFWriter.WriteLine("[END_SCENES]");
PFWriter.Flush();
}
// Write the project data into the file
using (StreamWriter PFWriter = new StreamWriter(ProjectConfigFilePath, false))
{
PFWriter.WriteLine("[PROJECT_INFO]");
PFWriter.WriteLine(ProjectName);

if (OpenScene.Contains(Path.GetDirectoryName(ProjectConfigFilePath)))
{
PFWriter.WriteLine($"INSIDE_PROJECT_DIR -> {OpenScene.Replace(Path.GetDirectoryName(ProjectConfigFilePath), "")}");
}
else
{
PFWriter.WriteLine(OpenScene);
}

PFWriter.WriteLine("[END_PROJECT_INFO]");
PFWriter.WriteLine("\n[SCENES]");

ConsoleUtils.StatusWrite(string.Join(", ", SceneList));

foreach (string Scene in SceneList)
{

if (Scene.Contains(Path.GetDirectoryName(ProjectConfigFilePath)))
{
PFWriter.WriteLine($"INSIDE_PROJECT_DIR -> {Scene.Replace(Path.GetDirectoryName(ProjectConfigFilePath), "")}");
}
else
{
PFWriter.WriteLine(Scene);
}
}

PFWriter.WriteLine("[END_SCENES]");
PFWriter.Flush();
}
9 Replies
Mazranel
MazranelOP4mo ago
This is what I got the last time I ran the code:
No description
Mazranel
MazranelOP4mo ago
And this is the expected output:
No description
TheBoxyBear
TheBoxyBear4mo ago
Are you overwriting a file? StreamWriter doesn't fulle overwrite the stream and only where you tell it. If the original file was longer, you'll have leftover lines You can mark the end of the file by setting the length of the writer's BaseStream Note that the stream must be seekable for that but when creating the writer this way, the stream is, however it may not be the case if creating the writer from an existing stream. Alternatively, you can write to a temp file and move it to the destination to replace the file
Mazranel
MazranelOP4mo ago
I am, but I'm clearing the file beforehand I'm setting the file's length to zero Even if there's no data in the file it produces output similar to this
Omnissiah
Omnissiah4mo ago
im not seeing this kind of bug BUT maybe this is not the code being executed maybe it's not duplicating a line, it's the other stuff that is empty maybe SceneList contains garbage BUT ABOVE ALL have you not debugged this?!
Mazranel
MazranelOP4mo ago
I've made sure that SceneList only contains proper data, and yes I've tried debugging. I have no clue what's going on, hence why I asked here. I've tried seeing if anyone else is having such issues, I've tried giving the filesystem some time to sync changes, I've even tried .asking am entirely new program to test the stream writer (which worked just fine) I'm pretty stumped
Omnissiah
Omnissiah4mo ago
if you have debugged this then are at what point in PFWriter is sent something invalid? you can even set a breakpoint
TheBoxyBear
TheBoxyBear4mo ago
To fully rule out filesystem issues, you can try creating the writer from a MemoryStream and convert the result to a string.
Mazranel
MazranelOP4mo ago
I'm going to try a different writer and see if anything changes, I forgot to do that during debugging Something like this Even using File.WriteAllLines has the same result Maybe I'm doing something wrong I've found the bug. It was a mix of the project compiling but refusing to write files to the disk for some reason, and (embarrassingly) SceneList contained some garbage because I wasn't correctly parsing project data files when opening them. When I printed the contents of SceneList from a different class, I didn't see the garbage though.
Want results from more Discord servers?
Add your server