如何在 C# 中将选择输出文本写入/附加到一个文件?
因此,我用 C# 创建了一个程序,该程序基本上跟踪棒球/垒球比赛的统计数据,而我要做的就是创建一个“游戏日志”,将游戏中的精彩时刻输出到文件中。例如,虽然我希望控制台上的输出来提示我的命令等(虽然我仍然希望它也显示游戏中的事件),但我也想编写那些游戏中的事件,例如球场、球将比赛、谁得分等信息放入我指定的一个特定文件中。
我尝试过使用 StreamWriter,但问题是:
- 我似乎只能允许我的文本输出到控制台或文件。
- 我不希望将所有文本输出到文件,只希望某些事件输出到文件。
因此,这是我的代码输出示例在控制台上的样子(粗体部分是我想要在指定输出文件中出现的内容以及控制台,斜体部分是我的输入)。请注意,此布局并不完全按照我想要的方式工作,但在我的控制台上看起来很好:
R H E LOB
Lady Sharks 0 0 0 0
Home Team 0 0 0 0
**Top 1**, 0 outs.
1B:
2B:
3B:
**1) K. Robinson #7** -- Today: 0 for 0
Count 0-0: *b*
**Ball.**
Count 1-0: *f*
**Foul Ball.**
Count 1-1: *p*
Ball is put in play.
Enter hit type (g/f): *g*
Enter hit zone (1-6): *4*
Enter result.
Choose from: 1b, 2b, 3b, hr, g, f, sh, sf, fc, e, gdp, di, bi
*1b*
Did the batter-runner advance any extra bases?
**In play, no out.
(1-1) K. Robinson singles on a ground ball to left field. 0 out.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 0 outs.
1B: K. Robinson #7
2B:
3B:
**2) S. Ribeiro #9** -- Today: 0 for 0
Count 0-0: *s*
**Swinging Strike.**
Count 0-1: *f*
**Foul Ball.**
Count 0-2: *s*
**Swinging Strike.**
Batter strikes out swinging. Confirm? (y/n)
*y*
Update status of runner on 1st, #7.
Batter may have reached base on exception. Indicate how many bases traveled with
"+" signs.
This will result in an out. Are you sure? (y/n)
*y*
**(0-2) S. Ribeiro strikes out swinging. 1 out.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 1 out.
1B: K. Robinson #7
2B:
3B:
**3) B. Sutton #11 -- Today: 0 for 0**
Count 0-0: *b*
**Ball.**
Count 1-0: *b*
**Ball.**
Count 2-0: *sb*
Select which base the runner attemped to steal from.
*1*
Runner steals second base. Confirm? (y/n)
*y*
**K. Robinson steals second base.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 1 out.
1B:
2B: K. Robinson #7
3B:
3) B. Sutton #11 -- Today: 0 for 0
Count 2-0: *p*
Ball is put in play.
Enter hit type (g/f): *g*
Enter hit zone (1-6): *3*
Enter result.
Choose from: 1b, 2b, 3b, hr, g, f, sh, sf, fc, e, gdp, di, bi
*sh*
Update status of runner on 2nd, #7.
*+*
Batter may have reached base on exception. Indicate how many bases traveled with
"+" signs.
This will result in an out. Are you sure? (y/n)
*y*
**In play, out(s).
(2-0) B. Sutton out on a sacrifice bunt. K. Robinson to 3rd. 2 outs.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 2 outs.
1B:
2B:
3B: K. Robinson #7
**4) S. Van Belleghem #29** -- Today: 0 for 0
Count 0-0: *p*
Ball is put in play.
Enter hit type (g/f): *f*
Enter hit zone (1-6): *5*
Enter result.
Choose from: 1b, 2b, 3b, hr, g, f, sh, sf, fc, e, gdp, di, bi
*f*
Update status of runner on 3rd, #7.
Middle of 1
**0 runs, 1 hit, 0 errors, 1 left on base**
Due up:
1) #1
2) #5
3) #3
Press enter to continue to Bottom 1.
因此,我只想将这些粗体部分写入一个文件,称为“Game 7 Log”。然后我会为每场比赛做一个,这样我就可以保存每场比赛的记录,逐个投球和逐个击球手。如果有人知道如何将这些选定的文本放入文件中,而不需要在控制台上删除任何信息,谢谢!
So I've created a program in C# that basically tracks statistics for a baseball/softball game, and all I want left to do is create a "game log" that outputs highlight moments in the game into a file. So for instance, while I want output on the console to prompt my commands and such (and while I still want it to display the events in the game as well), I also want to write those in-game events like the pitches, balls put in play, who scores, etc. into one specific file which I designate.
I've tried using StreamWriter, but the problem(s) are:
- I can only seem to allow my text to output either to the console or the file.
- I don't want all of my text to output to the file, only certain events.
So, here's what an example of my code output looks like on the console (the bolded sections are what I want to be in the designated output file AS WELL as the console, the italicized sections are my input). Note that this layout didn't exactly work as I wanted it to on here, but it looks fine on my console:
R H E LOB
Lady Sharks 0 0 0 0
Home Team 0 0 0 0
**Top 1**, 0 outs.
1B:
2B:
3B:
**1) K. Robinson #7** -- Today: 0 for 0
Count 0-0: *b*
**Ball.**
Count 1-0: *f*
**Foul Ball.**
Count 1-1: *p*
Ball is put in play.
Enter hit type (g/f): *g*
Enter hit zone (1-6): *4*
Enter result.
Choose from: 1b, 2b, 3b, hr, g, f, sh, sf, fc, e, gdp, di, bi
*1b*
Did the batter-runner advance any extra bases?
**In play, no out.
(1-1) K. Robinson singles on a ground ball to left field. 0 out.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 0 outs.
1B: K. Robinson #7
2B:
3B:
**2) S. Ribeiro #9** -- Today: 0 for 0
Count 0-0: *s*
**Swinging Strike.**
Count 0-1: *f*
**Foul Ball.**
Count 0-2: *s*
**Swinging Strike.**
Batter strikes out swinging. Confirm? (y/n)
*y*
Update status of runner on 1st, #7.
Batter may have reached base on exception. Indicate how many bases traveled with
"+" signs.
This will result in an out. Are you sure? (y/n)
*y*
**(0-2) S. Ribeiro strikes out swinging. 1 out.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 1 out.
1B: K. Robinson #7
2B:
3B:
**3) B. Sutton #11 -- Today: 0 for 0**
Count 0-0: *b*
**Ball.**
Count 1-0: *b*
**Ball.**
Count 2-0: *sb*
Select which base the runner attemped to steal from.
*1*
Runner steals second base. Confirm? (y/n)
*y*
**K. Robinson steals second base.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 1 out.
1B:
2B: K. Robinson #7
3B:
3) B. Sutton #11 -- Today: 0 for 0
Count 2-0: *p*
Ball is put in play.
Enter hit type (g/f): *g*
Enter hit zone (1-6): *3*
Enter result.
Choose from: 1b, 2b, 3b, hr, g, f, sh, sf, fc, e, gdp, di, bi
*sh*
Update status of runner on 2nd, #7.
*+*
Batter may have reached base on exception. Indicate how many bases traveled with
"+" signs.
This will result in an out. Are you sure? (y/n)
*y*
**In play, out(s).
(2-0) B. Sutton out on a sacrifice bunt. K. Robinson to 3rd. 2 outs.**
R H E LOB
Lady Sharks 0 1 0 0
Home Team 0 0 0 0
Top 1, 2 outs.
1B:
2B:
3B: K. Robinson #7
**4) S. Van Belleghem #29** -- Today: 0 for 0
Count 0-0: *p*
Ball is put in play.
Enter hit type (g/f): *f*
Enter hit zone (1-6): *5*
Enter result.
Choose from: 1b, 2b, 3b, hr, g, f, sh, sf, fc, e, gdp, di, bi
*f*
Update status of runner on 3rd, #7.
Middle of 1
**0 runs, 1 hit, 0 errors, 1 left on base**
Due up:
1) #1
2) #5
3) #3
Press enter to continue to Bottom 1.
So again, I only want those bolded parts written to a file, called say, "Game 7 Log". Then I'll do one for each game so that I can keep these records of each game, pitch for pitch and batter for batter. If anyone knows how I can put these selected texts into a file without taking out any information on my console, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你如何写入控制台。简单的Console.WriteLine?我会使用这样的方法:
创建一个您使用的帮助程序方法,而不是直接调用 Console.Writeline
定义一个标准,说明哪一行(“事件”)进入控制台,哪一行进入控制台和一个文件(这可以是一个单独的方法,方法上的 bool/enum 参数,或者给定字符串的某些字符) 定义一个标准,
一个非常基本的实现如下所示:
How do you write to the console. Simply Console.WriteLine? I would use an approach like this:
Create a Helper Method that you use instead of a direct call to Console.Writeline
Define a critera that says which line ("events") go to the console, and which go to the console and a file (this can be a seperate method, a bool/enum param on your method, or some characeterist of the string given to your method)
A very basic implementation looks like this: