开发者

How to write/append select output text to one file in C#?

开发者 https://www.devze.com 2023-03-09 16:49 出处:网络
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 fil

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. Robin开发者_如何学运维son 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!


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:

public void Write(string Message, bool AlsoWriteToFile) {
  if(AlsoWriteToFile) {
    using(StreamWriter s = new StreamWriter("filename.txt") {
      s.WriteLine(Message);
    }
  }
  Console.WriteLine(Message);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消