2

Right now I'm building a game application using c#, which will require loading from text file for the game script. (It's quite a simple visual novel game)

Now when the main form loads, i load the script from the file script.txt And i declared :

StringReader reader = new StringReader(script);

as a global variable there

Now in the middle of the game where the reader is in the middle of the string script, I need to append starting from next line of the reader. Basically what I want to achieve:

Append all texts from "news.txt" to script starting from reader.ReadLine() [i.e. in the middle of the string script]

What will be the most efficient solution to achieve this?

What I know:

StreamReader sr = new StreamReader("news.txt");
string news = sr.ReadToEnd();
//Now how to append 'news' to reader.ReadLine() ??

Edit for more clarification (sorry, this is my first time asking here) : I will try to explain more about what i'm trying to achieve here. What i'm having right now :

//global variables
string script;
StringReader reader;

//during form_load
StreamReader sr = new StreamReader("script.txt");
script = sr.ReadToEnd();
reader - new StringReader(script);

//And as the game progresses, I keep on implementing reader.ReadLine()..
//At one point, the program will ask the user, do you want to watch the news?
DialogResult dialogResult = MessageBox("Do you want to watch the news?", , MessageBoxButtons.YesNo

if(dialogResult == DialogResult.Yes)
{
   StreamReader newsSr = new StreamReader("news.txt");
   string news = newsSr.ReadToEnd();
   //now I want to append the contents of 'news' to the string 'script' after reader.ReadLine() - any best way to implement this?
}

One possible way (and i think it's the worst way as well) is by introducing one count variable, to get the starting position of the last reader.ReadLine(), and execute the desired result using Insert like follows : script = script.Insert(startIndex, news)

4

3 に答える 3

0

結局、私はこの問題を解決することができました。これは私が使用したものです(誰かが知りたい場合に備えて:))

//I'm using a switch statement, in case reader.ReadLine() == "#(morningnews)"
dialogResult = MessageBox.Show("Do you want to watch the news?", , MessageBoxButtons.YesNo);
if(dialogResult = DialogResult.Yes)
{
  StreamReader sr = new StreamReader(directoryName + "\\morningactivities\\morningnews1.txt");
  string news = sr.ReadToEnd();
  script = script.Replace("#(morningnews)", "#(morningnews)\n" + news);
  reader = new StringReader(script);
  while (reader.ReadLine() != "#(morningnews)")
    continue;
  loadNextScript();
}

助けてくれたみんなに感謝します。実際にこれを思いつくためのインスピレーションを与えてくれました。

于 2013-02-04T14:39:48.023 に答える
0

を使用してファイルをメモリにロードするだけですFile.ReadAllLines()

その後、リーダー、ライター、ストリームなどを気にせずに、文字列配列としてこれにアクセスできます。

例えば:

// load files as arrays
string[] scriptLinesArray = File.ReadAllLines("script.txt");
string[] newsLinesArray = File.ReadAllLines("news.txt");

// convert arrays to lists
var script = new List<string>(scriptLinesArray);
var news = new List<string>(newsLinesArray );

// append news list to script list
script.AddRange(news);
于 2013-02-04T14:14:20.783 に答える