简体   繁体   English

如何格式化Stream Writer写入文本文件?

[英]How can I format my Stream Writer to write to my text file?

The text file is being used to describe the state of the game on a web browser. 该文本文件用于描述Web浏览器上的游戏状态。 So I need to format my nameWriter.WriteLine to look something like. 所以我需要格式化nameWriter.WriteLine使其看起来像。

Output to text file:
playerOneName, playerTwoName, _ , _ , _ , _ , _ , _ , _ , _

I know this may sound like "Oh just writeLine this!" 我知道这听起来像是“哦,只写一行!” But no, the underscores are an empty field that are to be replace by my StreamWriter, it tracks the moves of the player in a tic tac toe web game. 但是不,下划线是一个空字段,将由我的StreamWriter代替,它在井字游戏网页游戏中跟踪玩家的移动。 What can I use instead of the underscore to make that space available for my read and write? 我可以使用什么代替下划线来使该空间可用于我的读写?

Here is my StreamWriter, right now I only have it adding the player name. 这是我的StreamWriter,现在我只添加了播放器名称。

Can you show me how to format the output to text? 您能告诉我如何将输出格式化为文本吗?

Maybe separate it in an array? 也许将其分成一个数组? and use a array DelimiterList to key out the commas? 并使用数组DelimiterList键入逗号?

    string[] lineParts... and reference the linePart[0-11] 
and then do a lineParts = line.Split(delimiterList)?

Here is my write code. 这是我的写代码。

private void WriteGame(string playerOneName, string playerTwoName, string[] cells)
    {
        StreamWriter gameStateWriter = null;
        StringBuilder sb = new StringBuilder();
        try
        {
            gameStateWriter = new StreamWriter(filepath, true);
            gameStateWriter.WriteLine(playerOneName + " , " + playerTwoName);
            string[] gameState = { playerOneName, 
            playerTwoName, null, null, null, null, 
    null, null, null, null, null };//I cannot use null, they will give me errors
            foreach (string GameState in gameState)
            {
                sb.Append(GameState);
                sb.Append(",");
            }
            gameStateWriter.WriteLine(sb.ToString());
        }
        catch (Exception ex)
        {
            txtOutcome.Text = "The following problem ocurred when writing to the file:\n"
               + ex.Message;
        }
        finally
        {
            if (gameStateWriter != null)
                gameStateWriter.Close();
        }
    }

Lastly if playerOneName is already in the text file, how do I specifically write playerTwoName after it and check that it is there? 最后,如果文本文件中已经存在playerOneName,我该如何在其后专门写入playerTwoName并检查其是否在那里?

Using Visual Studio '08 ASP.NET website and forms 使用Visual Studio '08 ASP.NET网站和表单

Firstly, define the fact that underscore is a special thing that means empty for you, and that commas are your delimiter: 首先,定义下划线是一个特殊的事实,这对您来说意味着空,而逗号是您的定界符:

const string EMPTY = "_";
const string DELIMITER = ",";

Secondly, don't write spaces between the comma and the values, that will just make your life more difficult later on: 其次,不要在逗号和值之间写空格,这会使以后的生活更加困难:

// removed spaces
gameStateWriter.WriteLine(playerOneName + DELIMITER + playerTwoName);

Now your GameState is ready to be created: 现在可以创建您的GameState了:

string[] gameState = { playerOneName, playerTwoName, EMPTY, EMPTY, EMPTY, EMPTY, 
                       EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,  };

To check if player two is already there, you need to open and read the existing file, and check to see if the second token is not empty. 要检查第二个玩家是否已经存在,您需要打开并读取现有文件,并检查第二个令牌是否不为空。 That is, if you've read the file; 也就是说,如果您已阅读文件;

var line = "..."; // read the file until the line that .StartsWith(playerOne)
var playerTwo = line.Split(DELIMITER)[1];

if (playerTwo == EMPTY)
{
     // need to store the real playerTwo, otherwise leave as is
}

You can keep your code, but instead of sb.Append(GameState); 您可以保留代码,而不要保留sb.Append(GameState); do sb.Append(GameState??"_"); sb.Append(GameState??"_"); in your current code. 在您当前的代码中。

"??" “?” is null-coalescing operator in C# - so result of null ?? "_" 在C#中是null-coalescing运算符 -所以结果为null ?? "_" null ?? "_" is "_" and "SomeValue"??"_" is "SomeValue". null ?? "_"是“ _”, "SomeValue"??"_"是“ SomeValue”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM