ItsMods
Help Problem reading/writing file - Printable Version

+- ItsMods (https://www.itsmods.com)
+-- Forum: Archive (https://www.itsmods.com/Forum-Archive.html)
+--- Forum: Archive (https://www.itsmods.com/Forum-Archive--64.html)
+--- Thread: Help Problem reading/writing file (/Thread-Help-Problem-reading-writing-file.html)



Problem reading/writing file - narkos - 08-11-2012

Hello,

I'm coding a small plugin for stats, and i got problems with reading/writing file.
I made 2 functions, 1 for reading the file, and 1 to write it.

Function to read:
Code:
private void ReadServerStats() { try { // Create a StreamReader to read the file StreamReader monStreamReader = new StreamReader(@"zKokosStats/server.stats"); // Get the first line string line = monStreamReader.ReadLine(); // Loop var needed lool ;) int loop = 0; // Read all liunes, line per line while (line != null) { // If this is not a comment line if (!line.StartsWith("//")) { string[] infos = line.Split('='); if (infos.Length == 2) { switch (infos[0]) { case "UpTime": ServerStats[0] = Int32.Parse(infos[1]); break; case "TotalKills": ServerStats[1] = Int32.Parse(infos[1]); break; case "MapsPlayed": ServerStats[2] = Int32.Parse(infos[1]); break; case "TotalConnections": ServerStats[3] = Int32.Parse(infos[3]); break; default: ServerPrint("Plugin zKokosStats: Unknown config option found '" + infos[0] + "'"); break; } ServerStats[loop] = Int32.Parse(infos[1]); } } line = monStreamReader.ReadLine(); loop += 1; } // Close the stream monStreamReader.Close(); } catch (Exception ex) { // Exception ServerPrint("Fucking exception:"+ex.Message); } }

Function to write:
Code:
private void WriteServerStats() { // create a writer and open the file try { StreamWriter tw = new StreamWriter(@"zKokosStats/server.stats"); ServerPrint("StatswillbeWritten2"); // write the serverstats contents tw.WriteLine("UpTime=" + ServerStats[0]); tw.WriteLine("TotalKills=" + ServerStats[1]); tw.WriteLine("MapsPlayed=" + ServerStats[2]); tw.WriteLine("TotalConnections=" + ServerStats[3]); // close the stream tw.Close(); } catch (Exception ex) { // Exception ServerPrint(ex.Message); } }

The function to read the stats file is called on "OnServerLoad".
The function to write the stats file is called on "OnPreMapChange".

The write function throw an exception that says "The file is already used by an other processus...".

If i take off the function to read, the write function work well.

So it seems li8ke the read function doesn't close the stats file stream... but in the code i close it so i don't understand what append.

I hope someone will be able to help me with this Wink

Thx in advance and sorry again for my english.


RE: Problem reading/writing file - Nekochan - 08-11-2012

I had same problem in my programs/games

Try to use '\\', not '/'.
I solved my problem by that way.

and why @"zKokosStats/server.stats" ?
Why not 'Environment.CurrentDirectory + "\\zKokosStats\\server.stats"

Also that problem could be because of extension. I used .log* extension, it given error, i changed to .txt* - it was fine


RE: Problem reading/writing file - narkos - 08-12-2012

With my code the file is properly readed because ingame i saw the good result, but when i want to save the infos the function says that the file is already used.... but if i comment the read function the file is written correctly.
So no problems with / or the file extension....
The problem i have is that the server.stats file still used by the process after the read function.

The function to read file comes from here: http://msdn.microsoft.com/fr-fr/library/aa287535%28v=vs.71%29.aspx and i just added @before the location cause i saw it in a few tuts.

But thx for your answer


RE: Problem reading/writing file - zxz0O0 - 08-12-2012

Try using the 'using statement' (http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx )

The function ReadServerStats does not output an exception?


RE: Problem reading/writing file - narkos - 08-12-2012

Yep i did a mistake! ... arfff vodka lol. And i didn't saw the exception before now lol ( @zxz0O0 yep the function returned an exception)
I changed this:
ServerStats[3] = Int32.Parse(infos[3]);
to this:
ServerStats[3] = Int32.Parse(infos[1]);

Now it works Wink

I'm making a plugin for server and player stats, i would like to store this data but i don't know what to choose between mysql or files.

On mysql it can be easy to use for a stats website, but it will need a mysql server for every server owner...
With file we will have to copy repetively the stats file to a http: readable location to be able to show them updated on a website...

What you think is better?