我如何格式化我流写入到写我的文本文件? [英] How can I format my Stream Writer to write to my text file?

查看:91
本文介绍了我如何格式化我流写入到写我的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的文本文件正被用来描述在网络浏览器的游戏的状态。所以,我需要格式化我nameWriter.WriteLine看起来像。

The text file is being used to describe the state of the game on a web browser. So I need to format my nameWriter.WriteLine to look something like.

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

我知道这听起​​来像哦,这只是的WriteLine!但是,没有,下划线是一个空的字段都出现在我的StreamWriter取代,它跟踪玩家在井字网页游戏的动作。我可以用什么来代替下划线,使可用空间为我的读写?

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. What can I use instead of the underscore to make that space available for my read and write?

下面是我的StreamWriter,现在我只有它加入的球员的名字。

Here is my StreamWriter, right now I only have it adding the player name.

也许在一个数组它分开?并使用数组DelimiterList键出逗号?

Maybe separate it in an array? and use a array DelimiterList to key out the commas?

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

下面是我写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();
        }
    }

最后,如果playerOneName已经在文本文件,我怎么专门之后写playerTwoName并检查它的存在?

使用Visual Studio '08 ASP.NET网站和形式

Using Visual Studio '08 ASP.NET website and forms

推荐答案

首先,定义强调的是,意味着空给你一个特别的东西,而事实上,逗号是您的分隔符:

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:

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
}

这篇关于我如何格式化我流写入到写我的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆