Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Problem reading/writing file
#1
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.
Reply

#2
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
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click
Reply

#3
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
Reply

#4
Try using the 'using statement' (http://msdn.microsoft.com/en-us/library/...80%29.aspx )

The function ReadServerStats does not output an exception?
[Image: azuw.jpg]
Reply

#5
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?
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Help Android problem Yamato 12 7,518 04-25-2014, 04:49
Last Post: ScHmIdTy56789
  Problem with Rain Effects on Maps mitchhacker 5 4,476 10-22-2013, 00:46
Last Post: mitchhacker
  Help mod not reading .iwd files? hmann 4 4,211 10-13-2013, 20:14
Last Post: hmann
  Help GSC Reading Files?! Howl3r 11 7,622 07-30-2013, 04:00
Last Post: DidUknowiPwn
  Help Liberation Problem Yamato 27 23,513 07-17-2013, 19:54
Last Post: feature
  [xna 4]Rendering problem narkos 9 5,329 07-03-2013, 19:00
Last Post: Nekochan
  Help Problem God plugin v4.0 4nonymous 1 2,550 06-22-2013, 23:25
Last Post: 8q4s8
Wink Preview Black ops 2 - CFG file dump SLiiTH3R 11 17,712 06-09-2013, 16:01
Last Post: dylankrajewski
  Help Game Not Reading Mod? Killjoy 6 5,074 06-08-2013, 14:49
Last Post: Killjoy
  [Release] Modern Warfare 3 Fast File Sounds master131 5 17,436 05-21-2013, 16:46
Last Post: ZURA.

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.