2013-02-04 26 views
2

現在我正在用c#構建一個遊戲應用程序,這將需要從遊戲腳本的文本文件中加載。 (這是一個相當簡單的視覺小說遊戲)用StringReader追加文本(reader.ReadLine()之後)

現在,當主窗體加載時,我加載該文件中的腳本script.txt 而且我宣佈:

StringReader reader = new StringReader(script); 

爲全局變量有

現在在閱讀器處於字符串腳本中間的遊戲中間,我需要從閱讀器的下一行開始追加。 基本上我想達到的目標:

追加來自「news.txt」腳本所有文本從reader.ReadLine()即開始在字符串腳本的中間]

什麼是最有效的解決方案?

我所知道的:

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

編輯更多的澄清(對不起,這是我第一次問這裏): 我會盡量解釋一下什麼,我想在這裏實現。 什麼,我有現在:

//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? 
} 

一種可能的方式(我認爲這是最糟糕的方式也一樣)是通過將一個計數變量,得到最後reader.ReadLine的起始位置() , 並執行所需的結果使用插入如下: script = script.Insert(startIndex,news)

+5

閱讀器 - 讀取,寫入器 - 寫入 – sll

+0

您確實需要附加到文件或「新聞」字符串嗎? –

+0

+ sll,我找不到任何方法reader.write或這樣的?幫幫我? @Daniel我需要將字符串'news'追加到字符串'script'而不是實際的'script.txt' 我打算做的是追加到字符串腳本,reader = new StringReader(Script) ,然後再運行一個循環直到當前行。 –

回答

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(); 
} 

感謝大家誰幫助,它給了我靈感,實際上這種走了過來。

0

您不能寫入StringReader

但是,如果我理解你的最新問題,我想你想要這個。

StreamReader sr = new StreamReader("news.txt"); 
string news = string.Empty; 
string line = sr.ReadLine(); 

while (line != null) 
{ 
    news += line; 
    news += someOtherString; 
    line = sr.ReadLine(); 
} 

除了我不會字符串連接做到這一點。我會用StringBuilder來做到這一點。

+0

我不認爲這是我正在尋找的解決方案。 我編輯了我的主要問題,以提供有關此問題的更多詳細信息。我很抱歉第一次不夠清楚。 同時我會嘗試搜索StringBuilder,謝謝:) –

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);