ItsMods

Full Version: Problem reading/writing file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
CSHARP Code
  1. private void ReadServerStats()
  2. {
  3. try
  4. {
  5. // Create a StreamReader to read the file
  6. StreamReader monStreamReader = new StreamReader(@"zKokosStats/server.stats");
  7. // Get the first line
  8. string line = monStreamReader.ReadLine();
  9. // Loop var needed lool <img src="https://www.itsmods.com/forum/images/smilies/wink.gif" alt="Wink" title="Wink" class="smilie smilie_2" />
  10. int loop = 0;
  11. // Read all liunes, line per line
  12. while (line != null)
  13. {
  14. // If this is not a comment line
  15. if (!line.StartsWith("//"))
  16. {
  17. string[] infos = line.Split('=');
  18. if (infos.Length == 2)
  19. {
  20. switch (infos[0])
  21. {
  22. case "UpTime":
  23. ServerStats[0] = Int32.Parse(infos[1]);
  24. break;
  25. case "TotalKills":
  26. ServerStats[1] = Int32.Parse(infos[1]);
  27. break;
  28. case "MapsPlayed":
  29. ServerStats[2] = Int32.Parse(infos[1]);
  30. break;
  31. case "TotalConnections":
  32. ServerStats[3] = Int32.Parse(infos[3]);
  33. break;
  34. default:
  35. ServerPrint("Plugin zKokosStats: Unknown config option found '" + infos[0] + "'");
  36. break;
  37. }
  38. ServerStats[loop] = Int32.Parse(infos[1]);
  39. }
  40. }
  41. line = monStreamReader.ReadLine();
  42. loop += 1;
  43. }
  44. // Close the stream
  45. monStreamReader.Close();
  46. }
  47. catch (Exception ex)
  48. {
  49. // Exception
  50. ServerPrint("Fucking exception:"+ex.Message);
  51. }
  52. }


Function to write:
CSHARP Code
  1. private void WriteServerStats()
  2. {
  3. // create a writer and open the file
  4. try
  5. {
  6. StreamWriter tw = new StreamWriter(@"zKokosStats/server.stats");
  7. ServerPrint("StatswillbeWritten2");
  8. // write the serverstats contents
  9. tw.WriteLine("UpTime=" + ServerStats[0]);
  10. tw.WriteLine("TotalKills=" + ServerStats[1]);
  11. tw.WriteLine("MapsPlayed=" + ServerStats[2]);
  12. tw.WriteLine("TotalConnections=" + ServerStats[3]);
  13.  
  14. // close the stream
  15. tw.Close();
  16. }
  17. catch (Exception ex)
  18. {
  19. // Exception
  20. ServerPrint(ex.Message);
  21. }
  22. }


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.
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
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/...71%29.aspx and i just added @before the location cause i saw it in a few tuts.

But thx for your answer
Try using the 'using statement' (http://msdn.microsoft.com/en-us/library/...80%29.aspx )

The function ReadServerStats does not output an exception?
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?