1

I have a txt file that is in my content folder for my XNA game. I have been able to read data from that file using the stream reader; however I was not able to write strings to it using the stream*writer* because it says I cannot write to the stream.

I use the content folder so that I may store high scores externally and that I may publish the game with the high score txt. I need to add the string "OEB 3350" to this file.

The location of my file is "Content\leaderboard.txt"

How does one write to this txt file.

public void updateExternalLeaderboard() 
    {
        string[] leaderboardArray = new string[orderedScoreList.Count];

        System.IO.Stream stream = TitleContainer.OpenStream("Content\\leaderboard.txt");
        System.IO.StreamWriter swriter= new System.IO.StreamWriter(stream);


        for (int p = 0; p < orderedScoreList.Count; p++) 
        {
            leaderboardArray[p] = orderedScoreList[p].initials + " " + orderedScoreList[p].scoreString;
            swriter.WriteLine(leaderboardArray[p]);
        }
        swriter.Close();

    }
Oliver Barnum
  • 75
  • 1
  • 10
  • You aren't supposed to write to the content folder / `titlecontainer`. Thats where you should store read-only things like textures and map files. It typically contains files pre-processed for quick loading through the content pipeline). – George Duckett Jul 16 '12 at 12:45
  • There where does one put files that have to be carried over from game to game? I don't want the highscores to disappear once you exit the program. – Oliver Barnum Jul 16 '12 at 12:48
  • See here: http://msdn.microsoft.com/en-us/library/bb203924(v=xnagamestudio.40).aspx or this question: [What is a good example of saving game data in XNA 4.0?](http://stackoverflow.com/q/3723287) Where the file actually ends up would depends on the version of windows and the platform (xbox/pc/phone) the game is running on. – George Duckett Jul 16 '12 at 12:53
  • You're telling me there is an enormously easy way read from a file but not to write to one? Thanks for the pointers though. I'll clock another 5 hours instead of 5 minutes – Oliver Barnum Jul 16 '12 at 12:59
  • If you want to do it within the XNA framework so it will work on all platforms then yes, it's more complex. If you only want it to work in your single case, then get the stream using standard .net methods, e.g. [`File.Create`](http://msdn.microsoft.com/en-us/library/system.io.file.create(v=vs.110).aspx) – George Duckett Jul 16 '12 at 13:04

1 Answers1

1

Check out Saving Data to a Game File

You have to serialize and use a storage container. The tutorial has a full project so it has alot of methods to do it.

Steve Blackwell
  • 5,904
  • 32
  • 49
Cyral
  • 13,999
  • 6
  • 50
  • 90